summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Jesse Jiryu Davis <jesse@mongodb.com>2019-09-13 16:32:30 +0000
committerevergreen <evergreen@mongodb.com>2019-09-13 16:32:30 +0000
commit9e978f6d7f1f304626f71bf6cf908cfaed08c19d (patch)
tree3eba558f95b1bafa4dbc49ad2b7c9a4e90a106d3
parent87d8a588a8f43c580f26f3c3031e507dea6c7e1e (diff)
downloadmongo-9e978f6d7f1f304626f71bf6cf908cfaed08c19d.tar.gz
SERVER-37690 Return 0 from countDocuments for empty collection
-rw-r--r--jstests/noPassthrough/document_count_functions.js5
-rw-r--r--src/mongo/shell/collection.js5
2 files changed, 8 insertions, 2 deletions
diff --git a/jstests/noPassthrough/document_count_functions.js b/jstests/noPassthrough/document_count_functions.js
index c1dd15ee591..207a6a65efa 100644
--- a/jstests/noPassthrough/document_count_functions.js
+++ b/jstests/noPassthrough/document_count_functions.js
@@ -12,6 +12,9 @@ const collName = "document_count_functions";
const coll = db.getCollection(collName);
coll.drop();
+assert.eq(0, coll.countDocuments({i: 1}));
+assert.commandWorked(db.createCollection(collName));
+assert.eq(0, coll.countDocuments({i: 1}));
assert.commandWorked(coll.insert({i: 1, j: 1}));
assert.commandWorked(coll.insert({i: 2, j: 1}));
@@ -55,4 +58,4 @@ assert.commandFailedWithCode(assert.throws(function() {
assert.commandWorked(db.adminCommand({configureFailPoint: 'maxTimeAlwaysTimeOut', mode: 'off'}));
MongoRunner.stopMongod(standalone);
-})(); \ No newline at end of file
+})();
diff --git a/src/mongo/shell/collection.js b/src/mongo/shell/collection.js
index 0774fa4d6c7..96bbdc9f9a9 100644
--- a/src/mongo/shell/collection.js
+++ b/src/mongo/shell/collection.js
@@ -1413,8 +1413,11 @@ DBCollection.prototype.countDocuments = function(query, options) {
// Format cursor into an array.
const res = this.aggregate(pipeline, aggregateOptions).toArray();
+ if (res.length) {
+ return res[0].n;
+ }
- return res[0].n;
+ return 0;
};
/**