Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions mongo/change_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/description"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/operation"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/session"
)

Expand Down Expand Up @@ -70,7 +69,7 @@ type ChangeStream struct {
// TryNext. If continued access is required, a copy must be made.
Current bson.Raw

aggregate *operation.Aggregate
aggregate *aggregateOp
pipelineSlice []bsoncore.Document
pipelineOptions map[string]bsoncore.Value
cursor changeStreamCursor
Expand Down Expand Up @@ -141,29 +140,38 @@ func newChangeStream(ctx context.Context, config changeStreamConfig, pipeline an
return nil, cs.Err()
}

cs.aggregate = operation.NewAggregate(nil).
ReadPreference(config.readPreference).ReadConcern(config.readConcern).
Deployment(cs.client.deployment).ClusterClock(cs.client.clock).
CommandMonitor(cs.client.monitor).Session(cs.sess).ServerSelector(cs.selector).
Retry(driver.RetryNone).MaxAdaptiveRetries(cursorOpts.MaxAdaptiveRetries).
EnableOverloadRetargeting(cursorOpts.EnableOverloadRetargeting).
ServerAPI(cs.client.serverAPI).Crypt(config.crypt).Timeout(cs.client.timeout).
Authenticator(cs.client.authenticator)
retry := driver.RetryNone
cs.aggregate = &aggregateOp{
readPreference: config.readPreference,
readConcern: config.readConcern,
deployment: cs.client.deployment,
clock: cs.client.clock,
monitor: cs.client.monitor,
session: cs.sess,
selector: cs.selector,
retry: &retry,
maxAdaptiveRetries: cursorOpts.MaxAdaptiveRetries,
enableOverloadRetargeting: cursorOpts.EnableOverloadRetargeting,
serverAPI: cs.client.serverAPI,
crypt: config.crypt,
timeout: cs.client.timeout,
authenticator: cs.client.authenticator,
}

if cs.options.Collation != nil {
cs.aggregate.Collation(bsoncore.Document(toDocument(cs.options.Collation)))
cs.aggregate.collation = bsoncore.Document(toDocument(cs.options.Collation))
}
if cs.options.Comment != nil {
comment, err := marshalValue(cs.options.Comment, cs.bsonOpts, cs.registry)
if err != nil {
return nil, err
}

cs.aggregate.Comment(comment)
cs.aggregate.comment = comment
cs.cursorOptions.Comment = comment
}
if cs.options.BatchSize != nil {
cs.aggregate.BatchSize(*cs.options.BatchSize)
cs.aggregate.batchSize = cs.options.BatchSize
cs.cursorOptions.BatchSize = *cs.options.BatchSize
}
if cs.options.MaxAwaitTime != nil {
Expand All @@ -182,7 +190,7 @@ func newChangeStream(ctx context.Context, config changeStreamConfig, pipeline an
}
customOptions[optionName] = optionValueBSON
}
cs.aggregate.CustomOptions(customOptions)
cs.aggregate.customOptions = customOptions
}
if cs.options.CustomPipeline != nil {
// Marshal all custom pipeline options before building pipeline slice. Return
Expand All @@ -201,11 +209,12 @@ func newChangeStream(ctx context.Context, config changeStreamConfig, pipeline an

switch cs.streamType {
case ClientStream:
cs.aggregate.Database("admin")
cs.aggregate.database = "admin"
case DatabaseStream:
cs.aggregate.Database(config.databaseName)
cs.aggregate.database = config.databaseName
case CollectionStream:
cs.aggregate.Collection(config.collectionName).Database(config.databaseName)
cs.aggregate.collection = config.collectionName
cs.aggregate.database = config.databaseName
default:
closeImplicitSession(cs.sess)
return nil, fmt.Errorf("must supply a valid StreamType in config, instead of %v", cs.streamType)
Expand All @@ -232,7 +241,7 @@ func newChangeStream(ctx context.Context, config changeStreamConfig, pipeline an
}
var pipelineArr bsoncore.Document
pipelineArr, cs.err = cs.pipelineToBSON()
cs.aggregate.Pipeline(pipelineArr)
cs.aggregate.pipeline = pipelineArr

if cs.err = cs.executeOperation(ctx, false); cs.err != nil {
closeImplicitSession(cs.sess)
Expand Down Expand Up @@ -298,7 +307,7 @@ func (cs *ChangeStream) executeOperation(ctx context.Context, resuming bool) err
_ = deployment.close()
}()

cs.aggregate.Deployment(deployment)
cs.aggregate.deployment = deployment

if resuming {
cs.replaceOptions(cs.wireVersion)
Expand All @@ -318,7 +327,7 @@ func (cs *ChangeStream) executeOperation(ctx context.Context, resuming bool) err
if plArr, cs.err = cs.pipelineToBSON(); cs.err != nil {
return cs.Err()
}
cs.aggregate.Pipeline(plArr)
cs.aggregate.pipeline = plArr
}

// Execute the aggregate, retrying on retryable errors once (1) if retryable reads are enabled and
Expand Down
93 changes: 54 additions & 39 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,44 +1044,46 @@ func aggregate(a aggregateParams, opts ...options.Lister[options.AggregateOption

cursorOpts.MarshalValueEncoderFn = newEncoderFn(a.bsonOpts, a.registry)

op := operation.NewAggregate(pipelineArr).
Session(sess).
WriteConcern(wc).
ReadConcern(rc).
ReadPreference(a.readPreference).
CommandMonitor(a.client.monitor).
Retry(retry).
MaxAdaptiveRetries(cursorOpts.MaxAdaptiveRetries).
EnableOverloadRetargeting(cursorOpts.EnableOverloadRetargeting).
ServerSelector(selector).
ClusterClock(a.client.clock).
Database(a.db).
Collection(a.col).
Deployment(a.client.deployment).
Crypt(a.client.cryptFLE).
ServerAPI(a.client.serverAPI).
HasOutputStage(hasOutputStage).
Timeout(a.client.timeout).
Authenticator(a.client.authenticator).
op := aggregateOp{
pipeline: pipelineArr,
session: sess,
writeConcern: wc,
readConcern: rc,
readPreference: a.readPreference,
monitor: a.client.monitor,
retry: &retry,
maxAdaptiveRetries: cursorOpts.MaxAdaptiveRetries,
enableOverloadRetargeting: cursorOpts.EnableOverloadRetargeting,
selector: selector,
clock: a.client.clock,
database: a.db,
collection: a.col,
deployment: a.client.deployment,
crypt: a.client.cryptFLE,
serverAPI: a.client.serverAPI,
hasOutputStage: hasOutputStage,
timeout: a.client.timeout,
authenticator: a.client.authenticator,
// Omit "maxTimeMS" from operations that return a user-managed cursor to
// prevent confusing "cursor not found" errors.
//
// See DRIVERS-2722 for more detail.
OmitMaxTimeMS(true)
omitMaxTimeMS: true,
}

if args.AllowDiskUse != nil {
op.AllowDiskUse(*args.AllowDiskUse)
op.allowDiskUse = args.AllowDiskUse
}
// ignore batchSize of 0 with $out
if args.BatchSize != nil && (*args.BatchSize != 0 || !hasOutputStage) {
op.BatchSize(*args.BatchSize)
op.batchSize = args.BatchSize
cursorOpts.BatchSize = *args.BatchSize
}
if args.BypassDocumentValidation != nil && *args.BypassDocumentValidation {
op.BypassDocumentValidation(*args.BypassDocumentValidation)
op.bypassDocumentValidation = args.BypassDocumentValidation
}
if args.Collation != nil {
op.Collation(bsoncore.Document(toDocument(args.Collation)))
op.collation = bsoncore.Document(toDocument(args.Collation))
}
if args.MaxAwaitTime != nil {
cursorOpts.SetMaxAwaitTime(*args.MaxAwaitTime)
Expand All @@ -1092,7 +1094,7 @@ func aggregate(a aggregateParams, opts ...options.Lister[options.AggregateOption
return nil, err
}

op.Comment(comment)
op.comment = comment
cursorOpts.Comment = comment
}
if args.Hint != nil {
Expand All @@ -1103,14 +1105,14 @@ func aggregate(a aggregateParams, opts ...options.Lister[options.AggregateOption
if err != nil {
return nil, err
}
op.Hint(hintVal)
op.hint = hintVal
}
if args.Let != nil {
let, err := marshal(args.Let, a.bsonOpts, a.registry)
if err != nil {
return nil, err
}
op.Let(let)
op.let = let
}
if args.Custom != nil {
// Marshal all custom options before passing to the aggregate operation. Return
Expand All @@ -1123,10 +1125,10 @@ func aggregate(a aggregateParams, opts ...options.Lister[options.AggregateOption
}
customOptions[optionName] = optionValueBSON
}
op.CustomOptions(customOptions)
op.customOptions = customOptions
}
if rawData, ok := optionsutil.Value(args.Internal, "rawData").(bool); ok {
op = op.RawData(rawData)
op.rawData = &rawData
}

err = op.Execute(a.ctx)
Expand Down Expand Up @@ -1199,22 +1201,35 @@ func (coll *Collection) CountDocuments(ctx context.Context, filter any,
maxAdaptiveRetries := coll.client.effectiveAdaptiveRetries(coll.client.retryReads)

selector := makeReadPrefSelector(sess, coll.readSelector, coll.client.localThreshold)
op := operation.NewAggregate(pipelineArr).Session(sess).ReadConcern(rc).ReadPreference(coll.readPreference).
Retry(retry).MaxAdaptiveRetries(maxAdaptiveRetries).
EnableOverloadRetargeting(coll.client.enableOverloadRetargeting).
CommandMonitor(coll.client.monitor).ServerSelector(selector).ClusterClock(coll.client.clock).Database(coll.db.name).
Collection(coll.name).Deployment(coll.client.deployment).Crypt(coll.client.cryptFLE).ServerAPI(coll.client.serverAPI).
Timeout(coll.client.timeout).Authenticator(coll.client.authenticator)
op := aggregateOp{
pipeline: pipelineArr,
session: sess,
readConcern: rc,
readPreference: coll.readPreference,
retry: &retry,
maxAdaptiveRetries: maxAdaptiveRetries,
enableOverloadRetargeting: coll.client.enableOverloadRetargeting,
monitor: coll.client.monitor,
selector: selector,
clock: coll.client.clock,
database: coll.db.name,
collection: coll.name,
deployment: coll.client.deployment,
crypt: coll.client.cryptFLE,
serverAPI: coll.client.serverAPI,
timeout: coll.client.timeout,
authenticator: coll.client.authenticator,
}
if args.Collation != nil {
op.Collation(bsoncore.Document(toDocument(args.Collation)))
op.collation = bsoncore.Document(toDocument(args.Collation))
}
if args.Comment != nil {
comment, err := marshalValue(args.Comment, coll.bsonOpts, coll.registry)
if err != nil {
return 0, err
}

op.Comment(comment)
op.comment = comment
}
if args.Hint != nil {
if isUnorderedMap(args.Hint) {
Expand All @@ -1224,10 +1239,10 @@ func (coll *Collection) CountDocuments(ctx context.Context, filter any,
if err != nil {
return 0, err
}
op.Hint(hintVal)
op.hint = hintVal
}
if rawData, ok := optionsutil.Value(args.Internal, "rawData").(bool); ok {
op = op.RawData(rawData)
op.rawData = &rawData
}

err = op.Execute(ctx)
Expand Down
Loading
Loading