Skip to content

Commit

Permalink
refactor(collection): use common method for decorating read concern
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Nov 20, 2017
1 parent 095b86e commit 5f499a4
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,7 @@ Collection.prototype.find = function() {
}

// Set the readConcern
if (this.s.readConcern) {
findCommand.readConcern = this.s.readConcern;
}
decorateWithReadConcern(findCommand, this, options);

// Decorate find command with collation options
decorateWithCollation(findCommand, this, options);
Expand Down Expand Up @@ -1958,9 +1956,7 @@ var count = function(self, query, options, callback) {
options = getReadPreference(self, options, self.s.db);

// Do we have a readConcern specified
if (self.s.readConcern) {
cmd.readConcern = self.s.readConcern;
}
decorateWithReadConcern(cmd, self, options);

// Have we specified collation
decorateWithCollation(cmd, self, options);
Expand Down Expand Up @@ -2020,9 +2016,7 @@ var distinct = function(self, key, query, options, callback) {
if (typeof maxTimeMS === 'number') cmd.maxTimeMS = maxTimeMS;

// Do we have a readConcern specified
if (self.s.readConcern) {
cmd.readConcern = self.s.readConcern;
}
decorateWithReadConcern(cmd, self, options);

// Have we specified collation
decorateWithCollation(cmd, self, options);
Expand Down Expand Up @@ -2409,6 +2403,15 @@ function decorateWithCollation(command, self, options) {
}
}

function decorateWithReadConcern(command, self, options) { // eslint-disable-line
let readConcern = {};
if (self.s.readConcern) {
Object.assign(readConcern, self.s.readConcern);
}

Object.assign(command, readConcern);
}

/**
* Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.2
* @method
Expand Down Expand Up @@ -2498,8 +2501,8 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
}

// Do we have a readConcern specified
if (!ignoreReadConcern && this.s.readConcern) {
command.readConcern = this.s.readConcern;
if (!ignoreReadConcern) {
decorateWithReadConcern(command, self, options);
}

// If we have allowDiskUse defined
Expand Down Expand Up @@ -2648,9 +2651,7 @@ var parallelCollectionScan = function(self, options, callback) {
};

// Do we have a readConcern specified
if (self.s.readConcern) {
commandObject.readConcern = self.s.readConcern;
}
decorateWithReadConcern(commandObject, self, options);

// Store the raw value
var raw = options.raw;
Expand Down Expand Up @@ -2737,9 +2738,7 @@ var geoNear = function(self, x, y, point, options, callback) {
commandObject = decorateCommand(commandObject, options, exclude);

// Do we have a readConcern specified
if (self.s.readConcern) {
commandObject.readConcern = self.s.readConcern;
}
decorateWithReadConcern(commandObject, self, options);

// Have we specified collation
decorateWithCollation(commandObject, self, options);
Expand Down Expand Up @@ -2794,9 +2793,7 @@ var geoHaystackSearch = function(self, x, y, options, callback) {
options = getReadPreference(self, options, self.s.db, self);

// Do we have a readConcern specified
if (self.s.readConcern) {
commandObject.readConcern = self.s.readConcern;
}
decorateWithReadConcern(commandObject, self, options);

// Execute the command
self.s.db.command(commandObject, options, function(err, res) {
Expand Down Expand Up @@ -2950,9 +2947,7 @@ var group = function(self, keys, condition, initial, reduce, finalize, command,
options = getReadPreference(self, options, self.s.db, self);

// Do we have a readConcern specified
if (self.s.readConcern) {
selector.readConcern = self.s.readConcern;
}
decorateWithReadConcern(selector, self, options);

// Have we specified collation
decorateWithCollation(selector, self, options);
Expand Down Expand Up @@ -3095,8 +3090,8 @@ var mapReduce = function(self, map, reduce, options, callback) {
options.readPreference = 'primary';
// Decorate command with writeConcern if supported
decorateWithWriteConcern(mapCommandHash, self, options);
} else if (self.s.readConcern) {
mapCommandHash.readConcern = self.s.readConcern;
} else {
decorateWithReadConcern(mapCommandHash, self, options);
}

// Is bypassDocumentValidation specified
Expand Down

0 comments on commit 5f499a4

Please sign in to comment.