Skip to content

Commit 6145d4b

Browse files
rosemaryydaprahamian
authored andcommitted
fix(createCollection): Db.createCollection should pass readConcern to new collection (#2026)
* add tests to verify correct readConcern inheritance * create new test for createCollection * modernized test
1 parent 4df8399 commit 6145d4b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lib/db.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ Db.prototype.createCollection = deprecateOptions(
505505
if (typeof options === 'function') (callback = options), (options = {});
506506
options = options || {};
507507
options.promiseLibrary = options.promiseLibrary || this.s.promiseLibrary;
508-
508+
options.readConcern = options.readConcern
509+
? new ReadConcern(options.readConcern.level)
510+
: this.readConcern;
509511
const createCollectionOperation = new CreateCollectionOperation(this, name, options);
510512

511513
return executeOperation(this.s.topology, createCollectionOperation, callback);

lib/operations/create_collection.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const illegalCommandFields = [
2222
'raw',
2323
'readPreference',
2424
'session',
25+
'readConcern',
2526
'writeConcern'
2627
];
2728

test/functional/readconcern_tests.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('ReadConcern', function() {
1010
return setupDatabase(configuration);
1111
});
1212

13-
it('Should set local readConcern on db level', {
13+
it('Should set local readConcern on db level when using collection method', {
1414
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },
1515

1616
test: function(done) {
@@ -56,6 +56,31 @@ describe('ReadConcern', function() {
5656
}
5757
});
5858

59+
it('Should set local readConcern on db level when using createCollection method', {
60+
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },
61+
62+
test: function(done) {
63+
// Get a new instance
64+
const configuration = this.configuration;
65+
const client = configuration.newClient(
66+
{ w: 1 },
67+
{ poolSize: 1, readConcern: { level: 'local' } }
68+
);
69+
client.connect((err, client) => {
70+
expect(err).to.not.exist;
71+
const db = client.db(configuration.db);
72+
expect(db.s.readConcern).to.deep.equal({ level: 'local' });
73+
74+
// Get a collection using createCollection
75+
db.createCollection('readConcernCollection', (err, collection) => {
76+
// Validate readConcern
77+
expect(collection.s.readConcern).to.deep.equal({ level: 'local' });
78+
client.close(done);
79+
});
80+
});
81+
}
82+
});
83+
5984
it('Should set majority readConcern on db level', {
6085
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },
6186

0 commit comments

Comments
 (0)