From 6bbb8de0e92e9dae3bb75a85e9c122e4fb39287d Mon Sep 17 00:00:00 2001 From: Brett Ausmeier Date: Thu, 4 May 2017 13:26:03 +0200 Subject: [PATCH 1/3] Test passing readConcern option to connect Add a test for passing the readConcern option to the MongoClient connect method. The option should be passed through to the db. --- test/functional/readconcern_tests.js | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/functional/readconcern_tests.js b/test/functional/readconcern_tests.js index f0fe8ab373..b010a71294 100644 --- a/test/functional/readconcern_tests.js +++ b/test/functional/readconcern_tests.js @@ -236,6 +236,49 @@ exports['Should set majority readConcern using MongoClient'] = { } } +exports['Should set majority readConcern using MongoClient with options'] = { + metadata: { requires: { topology: 'replicaset', mongodb: ">= 3.1.7" } }, + + test: function(configuration, test) { + var MongoClient = configuration.require.MongoClient; + var listener = require('../..').instrument(function(err, instrumentations) {}); + // Contains all the apm events + var started = []; + var url = configuration.url(); + var options = { + readConcern: { + level: 'majority' + } + } + + // Connect using mongoclient + MongoClient.connect(url, options, function(err, db) { + test.equal(null, err); + test.deepEqual({level: 'majority'}, db.s.readConcern); + + // Get a collection + var collection = db.collection('readConcernCollection'); + // Validate readConcern + test.deepEqual({level: 'majority'}, collection.s.readConcern); + // Perform a find using the readConcern + listener.on('started', function(event) { + if(event.commandName == 'find') + started.push(event); + }); + + // Execute find + collection.find().toArray(function(err, r) { + test.equal(1, started.length); + test.deepEqual({level:'majority'}, started[0].command.readConcern); + + listener.uninstrument(); + db.close(); + test.done(); + }); + }); + } +} + exports['Should error out with readConcern level set to majority'] = { metadata: { requires: { topology: 'replicaset', mongodb: "<= 3.0.X" } }, From 6aea4da510ba4391f58cba6d7c174ecec24b5186 Mon Sep 17 00:00:00 2001 From: Brett Ausmeier Date: Thu, 4 May 2017 14:06:30 +0200 Subject: [PATCH 2/3] Don't merge readConcern option The readConcern option is an object which contains a level property and should be passed through as-is. --- lib/mongo_client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo_client.js b/lib/mongo_client.js index 8440b1b61a..dfea46c65f 100644 --- a/lib/mongo_client.js +++ b/lib/mongo_client.js @@ -245,7 +245,7 @@ var mergeOptions = function(target, source, flatten) { var createUnifiedOptions = function(finalOptions, options) { var childOptions = ['mongos', 'server', 'db' , 'replset', 'db_options', 'server_options', 'rs_options', 'mongos_options']; - var noMerge = []; + var noMerge = ['readconcern']; for(var name in options) { if(noMerge.indexOf(name.toLowerCase()) != -1) { From 38448dd5ba01707a83935254894204c1ca519487 Mon Sep 17 00:00:00 2001 From: Brett Ausmeier Date: Thu, 4 May 2017 14:10:31 +0200 Subject: [PATCH 3/3] Fix types in documentation The level property of the readConcern object is a string, not an object. --- lib/mongo_client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mongo_client.js b/lib/mongo_client.js index dfea46c65f..4dfdc8d342 100644 --- a/lib/mongo_client.js +++ b/lib/mongo_client.js @@ -121,7 +121,7 @@ function MongoClient() { * @param {object} [options.pkFactory=null] A primary key factory object for generation of custom _id keys. * @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible * @param {object} [options.readConcern=null] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) - * @param {object} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) + * @param {string} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) * @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed); * @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug) * @param {object} [options.logger=undefined] Custom logger object @@ -183,7 +183,7 @@ var define = MongoClient.define = new Define('MongoClient', MongoClient, false); * @param {object} [options.pkFactory=null] A primary key factory object for generation of custom _id keys. * @param {object} [options.promiseLibrary=null] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible * @param {object} [options.readConcern=null] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) - * @param {object} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) + * @param {string} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) * @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed); * @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug) * @param {object} [options.logger=undefined] Custom logger object