...
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 asya 8 hours ago what happens when you run aggregate command and {$count:'c'} Danilo Nobrega 8 hours ago > db.data.aggregate({$count:‘c’}) { “c” : 10000 } Danilo Nobrega 8 hours ago 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”: {“$sum”: 1} }}); // countDocument options other than filter, skip, and limit, are added to the aggregate command. let aggregateOptions = {}; if (options.hint) { aggregateOptions.hint = 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 res[0].n; } 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 >
danilo.nobrega commented on Tue, 13 Oct 2020 12:25:56 +0000: jonathan.streets alright. I thought I would flag it as a bug, but if it is clear in the documentation, I guess that's acceptable. jonathan.streets commented on Mon, 12 Oct 2020 14:36:10 +0000: Hi danilo.nobrega We haven’t heard back from you for some time, so I’m going to mark this ticket as resolved. If this is still an issue for you, please provide additional information and we will reopen the ticket. Regards, Jon jonathan.streets commented on Mon, 13 Jul 2020 20:26:55 +0000: Issue: db.collection.countDocuments() with no arguments returns an error. The documentation for countDocuments() states in the examples section, to get a count of documents one can use db.orders.countDocuments({}) e.g. The following mongo shell commands complete successfully: db.feed.countDocuments({}) db.feed.count() The following command db.feed.countDocuments() returns an error { "ok" : 0, "errmsg" : "the match filter must be an expression in an object", "code" : 15959, "codeName" : "Location15959" } danilo.nobrega, i think this is how the command is meant to work ?