-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Shell
-
None
-
ALL
-
None
-
3
-
None
-
None
-
None
-
None
-
None
-
None
The shell does not set the query to {} for countDocuments() if it's empty.
i.e. the following returns an error from ADL as it is converted to a $match with no document passed in.
> db.data.countDocuments()
countDocuments() doesn’t seem to work with federated queries:> db.data.count()
10000
> db.data.countDocuments()
2020-06-25T00:12:36.334+0200 E QUERY [js] uncaught exception: Error: command failed: {
{{ "ok" : 0,}}
{{ "errmsg" : "failed parsing stage: $match stage must have a document as its only argument, correlationID = 161b9a7d52a6363a253a8c3e",}}
{{ "code" : 9,}}
{{ "codeName" : "FailedToParse"}}
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:583:17
assert.commandWorked@src/mongo/shell/assert.js:673:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12
DBCollection.prototype.countDocuments@src/mongo/shell/collection.js:1423:17
@(shell):1:1
18 replies
what happens when you run aggregate command and {$count:'c'}
Danilo Nobrega 8 hours ago
> db.data.aggregate({$count:‘c’})
{ “c” : 10000 }it works
asya 8 hours ago
that's all that the shell helper does for countDocuments
asya 8 hours ago
can you type db.data.countDocuments without the () and paste what you see?
asya 8 hours ago
oh
asya 8 hours ago
I bet I know - you need to run it as db.data.countDocuments({}) I bet.
asya 8 hours ago
bug in the shell
asya 8 hours ago
query should be `query = query || {} ` same as options.
asya 8 hours ago
it's a bug in the shell component.
asya 8 hours ago
need to file a Jira ticket on it.
asya 8 hours ago
surprised if there isn't one already though but then why didn't we fix it?
Danilo Nobrega 22 minutes ago
> db.data.countDocuments
function(query, options)
{ “use strict”; let pipeline = [
{“$match”: query}];
options = options || {};
assert.eq(typeof options, “object”, “‘options’ argument must be an object”); if (options.skip)
{ pipeline.push(
{“$skip”: options.skip});
}
if (options.limit)
{ pipeline.push(
{“$limit”: options.limit});
} // Construct an aggregation pipeline stage with sum to calculate the number of all documents.
pipeline.push({“$group”: {“_id”: null, “n”:
}}); // countDocument options other than filter, skip, and limit, are added to the aggregate command.
let aggregateOptions = {}; if (options.hint)
if (options.maxTimeMS)
{ aggregateOptions.maxTimeMS = options.maxTimeMS; }if (options.collation)
{ aggregateOptions.collation = options.collation; } // Format cursor into an array.
const res = this.aggregate(pipeline, aggregateOptions).toArray();
if (res.length)
return 0;
}
>
asya 21 minutes ago
Yeah it’s missing a line. Should set query to {} if it’s empty.
Danilo Nobrega 21 minutes ago
> db.data.countDocuments({})
10000
>