diff --git a/lib/collection.js b/lib/collection.js index 00b3ce58c1..8ecc7066cf 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -2426,6 +2426,7 @@ function decorateWithCollation(command, self, options) { * @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types. * @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers. * @param {object} [options.collation=null] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). + * @param {string} [options.comment] Add a comment to an aggregation command * @param {ClientSession} [options.session] optional session to use for this operation * @param {Collection~resultCallback} callback The command result callback * @return {(null|AggregationCursor)} @@ -2515,6 +2516,8 @@ Collection.prototype.aggregate = function(pipeline, options, callback) { // If explain has been specified add it if (options.explain) command.explain = options.explain; + if (typeof options.comment === 'string') command.comment = options.comment; + // Validate that cursor options is valid if (options.cursor != null && typeof options.cursor !== 'object') { throw toError('cursor options must be an object'); diff --git a/test/functional/aggregation_tests.js b/test/functional/aggregation_tests.js index 063a5e7b82..60ed07a203 100644 --- a/test/functional/aggregation_tests.js +++ b/test/functional/aggregation_tests.js @@ -1081,6 +1081,45 @@ describe('Aggregation', function() { } ); + it('should pass a comment down via the aggregation command', { + metadata: { + requires: { + mongodb: '>=3.5.0', + topology: 'single', + node: '>=4.8.5' + } + }, + test: function(done) { + const client = this.configuration.newClient({ w: 1 }, { poolSize: 1 }); + const databaseName = this.configuration.db; + + const comment = 'Darmok and Jalad at Tanagra'; + + client.connect(function(err, client) { + expect(err).to.be.null; + + const db = client.db(databaseName); + const collection = db.collection('testingPassingDownTheAggregationCommand'); + + const command = db.command; + + db.command = function(c) { + expect(c).to.be.an('object'); + expect(c.comment) + .to.be.a('string') + .and.to.equal('comment'); + command.apply(db, Array.prototype.slice.call(arguments, 0)); + }; + + collection.aggregate([{ $project: { _id: 1 } }], { comment }, function(err, r) { + expect(err).to.be.null; + expect(r).to.not.be.null; + done(); + }); + }); + } + }); + /** * Correctly call the aggregation framework to return a cursor with batchSize 1 and get the first result using next *