Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(createCollection): Db.createCollection should pass readConcern to new collection #2026

Merged
merged 13 commits into from
Jun 21, 2019
4 changes: 3 additions & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/operations/create_collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const illegalCommandFields = [
'raw',
'readPreference',
'session',
'writeConcern'
rosemaryy marked this conversation as resolved.
Show resolved Hide resolved
'readConcern'
];

class CreateCollectionOperation extends CommandOperation {
Expand Down
29 changes: 28 additions & 1 deletion test/functional/readconcern_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -56,6 +56,33 @@ 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' }, monitorCommands: true }
rosemaryy marked this conversation as resolved.
Show resolved Hide resolved
);

client.connect(function(err, client) {
rosemaryy marked this conversation as resolved.
Show resolved Hide resolved
expect(err).to.not.exist;

const db = client.db(configuration.db);
test.deepEqual({ level: 'local' }, db.s.readConcern);
rosemaryy marked this conversation as resolved.
Show resolved Hide resolved

// Get a collection using createCollection
const collection = db.createCollection('readConcernCollection');
// Validate readConcern
expect(test).to.deep.equal({ level: 'local' }, collection.s.readConcern);
rosemaryy marked this conversation as resolved.
Show resolved Hide resolved
client.close();
rosemaryy marked this conversation as resolved.
Show resolved Hide resolved
done();
rosemaryy marked this conversation as resolved.
Show resolved Hide resolved
});
}
});

it('Should set majority readConcern on db level', {
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },

Expand Down