summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Reams <jbreams@mongodb.com>2015-10-29 15:04:05 -0400
committerJonathan Reams <jbreams@mongodb.com>2015-11-12 15:54:52 -0500
commit80f0a56bfb80c17bf6edc8a35d7cab2f0c6016b0 (patch)
tree59d956e445a332cdc9e39ea1a63ef96ce2628de7
parent18c8ebacb0e90795dc8e83bf419c2cccad229026 (diff)
downloadmongo-80f0a56bfb80c17bf6edc8a35d7cab2f0c6016b0.tar.gz
SERVER-20257 Support readConcern in count command shell helper
-rw-r--r--jstests/core/crud_api.js2
-rw-r--r--jstests/noPassthrough/read_concern_helper.js4
-rw-r--r--src/mongo/shell/collection.js52
-rw-r--r--src/mongo/shell/query.js3
4 files changed, 25 insertions, 36 deletions
diff --git a/jstests/core/crud_api.js b/jstests/core/crud_api.js
index a4c49d965d5..2e696f7c615 100644
--- a/jstests/core/crud_api.js
+++ b/jstests/core/crud_api.js
@@ -642,7 +642,7 @@
// Simple count no arguments, applying hint
countExecutor({
insert: [{ _id: 1, x:11 }, { _id: 2, x:22 }, { _id: 3, x:33 }],
- params: [{}, {hint: "_id"}],
+ params: [{}, {hint: { "_id": 1}}],
result: 3,
expected: [{ _id: 1, x:11 }, { _id: 2, x:22 }, { _id: 3, x:33 }]
});
diff --git a/jstests/noPassthrough/read_concern_helper.js b/jstests/noPassthrough/read_concern_helper.js
index d268f4889ad..09146092629 100644
--- a/jstests/noPassthrough/read_concern_helper.js
+++ b/jstests/noPassthrough/read_concern_helper.js
@@ -14,6 +14,8 @@ var coll = testServer.getDB("test").readMajority;
assert.writeOK(coll.insert({_id: "foo"}));
assert.throws(function() { coll.find({_id: "foo"}).readConcern("majority").itcount(); });
assert.throws(function() { coll.findOne({_id: "foo"}, {}, {}, "majority"); });
+assert.throws(function() { coll.count({_id: "foo"}, {readConcern: "majority"}); });
+assert.throws(function() { coll.find({_id: "foo"}).readConcern("majority").count(); });
MongoRunner.stopMongod(testServer);
testServer = MongoRunner.runMongod({
@@ -25,6 +27,8 @@ coll = testServer.getDB("test").readMajority;
assert.doesNotThrow(function() { coll.find({_id: "foo"}).readConcern("majority").itcount(); });
assert.doesNotThrow(function() { coll.findOne({_id: "foo"}, {}, {}, "majority"); });
+assert.doesNotThrow(function() { coll.count({_id: "foo"}, {readConcern: "majority"}); });
+assert.doesNotThrow(function() { coll.find({_id: "foo"}).readConcern("majority").count(); });
MongoRunner.stopMongod(testServer);
}());
diff --git a/src/mongo/shell/collection.js b/src/mongo/shell/collection.js
index 65a31a1cb3e..81b2ec98dd6 100644
--- a/src/mongo/shell/collection.js
+++ b/src/mongo/shell/collection.js
@@ -1578,53 +1578,35 @@ DBCollection.prototype.unsetWriteConcern = function() {
* @param {number} [options.skip=null] The number of documents to skip for the count.
* @param {string|object} [options.hint=null] An index name hint or specification for the query.
* @param {number} [options.maxTimeMS=null] The maximum amount of time to allow the query to run.
+* @param {string} [options.readConcern=null] The level of readConcern passed to the count command
* @return {number}
*/
DBCollection.prototype.count = function(query, options) {
var opts = Object.extend({}, options || {});
- // Set parameters
- var skip = (typeof opts.skip == 'number') ? opts.skip : null;
- var limit = (typeof opts.limit == 'number') ? opts.limit : null;
- var maxTimeMS = (typeof opts.maxTimeMS == 'number') ? opts.maxTimeMS : null;
- var hint = opts.hint;
-
- // Execute using command if we have passed in skip/limit or hint
- if (skip != null || limit != null || hint != null || maxTimeMS != null) {
- // Final query
- var cmd = {
- 'count': this.getName(),
- 'query': query
- };
-
- // Add limit and skip if defined
- if (typeof skip == 'number') {
- cmd.skip = skip;
- }
-
- if (typeof limit == 'number') {
- cmd.limit = limit;
- }
+ var query = this.find(query);
+ if (typeof opts.skip == 'number') {
+ query.skip(opts.skip);
+ }
- if (hint) {
- opts.hint = hint;
- }
+ if (typeof opts.limit == 'number') {
+ query.limit(opts.limit);
+ }
- if (opts.maxTimeMS) {
- cmd.maxTimeMS = opts.maxTimeMS;
- }
+ if (typeof opts.maxTimeMS == 'number') {
+ query.maxTimeMS(opts.maxTimeMS);
+ }
- // Run the command and return the result
- var response = this.runReadCommand(cmd);
- if (response.ok == 0) {
- throw new Error("count failed: " + tojson(response));
- }
+ if (opts.hint) {
+ query.hint(opts.hint);
+ }
- return response.n;
+ if (typeof opts.readConcern == 'string') {
+ query.readConcern(opts.readConcern);
}
// Return the result of the find
- return this.find(query).count();
+ return query.count(true);
}
/**
diff --git a/src/mongo/shell/query.js b/src/mongo/shell/query.js
index c33ce619f47..50caf0330dd 100644
--- a/src/mongo/shell/query.js
+++ b/src/mongo/shell/query.js
@@ -331,6 +331,9 @@ DBQuery.prototype._convertToCountCmd = function( applySkipLimit ) {
if ( this._query.$hint ) {
cmd.hint = this._query.$hint;
}
+ if ( this._query.readConcern ) {
+ cmd.readConcern = this._query.readConcern;
+ }
}
else {
cmd.query = this._query;