diff --git a/lib/db.js b/lib/db.js index e5788da5147..9d7b30def2c 100644 --- a/lib/db.js +++ b/lib/db.js @@ -505,7 +505,9 @@ Db.prototype.createCollection = deprecateOptions( if (typeof options === 'function') (callback = options), (options = {}); options = options || {}; options.promiseLibrary = options.promiseLibrary || this.s.promiseLibrary; - + options.readConcern = options.readConcern + ? new ReadConcern(options.readConcern.level) + : this.readConcern; const createCollectionOperation = new CreateCollectionOperation(this, name, options); return executeOperation(this.s.topology, createCollectionOperation, callback); diff --git a/lib/operations/create_collection.js b/lib/operations/create_collection.js index 055f7efccb3..35c3a6f0763 100644 --- a/lib/operations/create_collection.js +++ b/lib/operations/create_collection.js @@ -22,6 +22,7 @@ const illegalCommandFields = [ 'raw', 'readPreference', 'session', + 'readConcern', 'writeConcern' ]; diff --git a/test/functional/readconcern_tests.js b/test/functional/readconcern_tests.js index 601fbb72412..97382c2c124 100644 --- a/test/functional/readconcern_tests.js +++ b/test/functional/readconcern_tests.js @@ -10,7 +10,7 @@ describe('ReadConcern', function() { return setupDatabase(configuration); }); - it('Should set local readConcern on db level', { + it('Should set local readConcern on db level when using collection method', { metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } }, test: function(done) { @@ -56,6 +56,31 @@ describe('ReadConcern', function() { } }); + it('Should set local readConcern on db level when using createCollection method', { + metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } }, + + test: function(done) { + // Get a new instance + const configuration = this.configuration; + const client = configuration.newClient( + { w: 1 }, + { poolSize: 1, readConcern: { level: 'local' } } + ); + client.connect((err, client) => { + expect(err).to.not.exist; + const db = client.db(configuration.db); + expect(db.s.readConcern).to.deep.equal({ level: 'local' }); + + // Get a collection using createCollection + db.createCollection('readConcernCollection', (err, collection) => { + // Validate readConcern + expect(collection.s.readConcern).to.deep.equal({ level: 'local' }); + client.close(done); + }); + }); + } + }); + it('Should set majority readConcern on db level', { metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },