Skip to content

Commit

Permalink
Merge pull request #1514 from bausmeier/fix-read-concern
Browse files Browse the repository at this point in the history
Fix passing the readConcern option to MongoClient.connect
  • Loading branch information
christkv committed May 4, 2017
2 parents f5e2a68 + 38448dd commit a732eab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions test/functional/readconcern_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" } },

Expand Down

0 comments on commit a732eab

Please sign in to comment.