diff --git a/lib/operations/aggregate.js b/lib/operations/aggregate.js index 395407b8ff8..6386eaa19a3 100644 --- a/lib/operations/aggregate.js +++ b/lib/operations/aggregate.js @@ -38,6 +38,16 @@ class AggregateOperation extends CommandOperationV2 { if (this.hasWriteStage) { this.readPreference = ReadPreference.primary; } + + if (options.explain && (this.readConcern || this.writeConcern)) { + throw new MongoError( + '"explain" cannot be used on an aggregate call with readConcern/writeConcern' + ); + } + + if (options.cursor != null && typeof options.cursor !== 'object') { + throw new MongoError('cursor options must be an object'); + } } get canRetryRead() { @@ -52,6 +62,7 @@ class AggregateOperation extends CommandOperationV2 { const command = { aggregate: this.target, pipeline: this.pipeline }; if ( + this.readConcern && (!this.hasWriteStage || serverWireVersion >= MIN_WIRE_VERSION_$OUT_READ_CONCERN_SUPPORT) && !inTransaction ) { @@ -85,16 +96,6 @@ class AggregateOperation extends CommandOperationV2 { } if (options.explain) { - if (command.readConcern || command.writeConcern) { - callback( - new MongoError( - '"explain" cannot be used on an aggregate call with readConcern/writeConcern' - ) - ); - - return; - } - command.explain = options.explain; } @@ -102,12 +103,6 @@ class AggregateOperation extends CommandOperationV2 { command.comment = options.comment; } - // Validate that cursor options is valid - if (options.cursor != null && typeof options.cursor !== 'object') { - callback(new MongoError('cursor options must be an object')); - return; - } - command.cursor = options.cursor || {}; if (options.batchSize && !this.hasWriteStage) { command.cursor.batchSize = options.batchSize;