[SERVER-37690] countDocuments throws an error when matching 0 document Created: 20/Oct/18  Updated: 29/Oct/23  Resolved: 20/Aug/19

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 4.0.3, 4.1.4
Fix Version/s: 4.0.13, 4.2.1, 4.3.1

Type: Bug Priority: Minor - P4
Reporter: Xavier GUIHOT Assignee: A. Jesse Jiryu Davis
Resolution: Fixed Votes: 2
Labels: query-44-grooming, storch
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Documented
is documented by DOCS-12966 Investigate changes in SERVER-37690: ... Closed
Related
is related to SERVER-40718 Validate parameters for estimatedDocu... Closed
Backwards Compatibility: Minor Change
Operating System: ALL
Backport Requested:
v4.2, v4.0
Steps To Reproduce:

```

use count

db.count.insert([\{a: 1}, \{a: 2}])

db.count.count({a:1}) // returns 1

db.count.count({a:3}) // returns 0

db.count.countDocuments({a:1}) // returns 1

db.count.countDocuments({a:3}) // throws an exception

```

Participants:

 Description   

`countDocuments` throws an error when matching 0 document.

I would have expected it to follow what `count` does, which is return 0.

With both versions `4.0.3` and `4.1.4` (haven't checked intermediary ones), the error I get from the bellow `steps to reproduce` is:

2018-10-20T16:26:44.252+0100 E QUERY [js] TypeError: res[0] is undefined :
 DBCollection.prototype.countDocuments@src/mongo/shell/collection.js:1418:5
 @(shell):1:1
 



 Comments   
Comment by Githook User [ 13/Sep/19 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis', 'email': 'jesse@mongodb.com'}

Message: SERVER-37690 Return 0 from countDocuments for empty collection
Branch: v4.0
https://github.com/mongodb/mongo/commit/d4f511978cbd5e425c0941cd3cda2cf9d5e03716

Comment by Githook User [ 13/Sep/19 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis', 'email': 'jesse@mongodb.com'}

Message: SERVER-37690 Return 0 from countDocuments for empty collection
Branch: v4.2
https://github.com/mongodb/mongo/commit/9e978f6d7f1f304626f71bf6cf908cfaed08c19d

Comment by David Storch [ 31/Aug/19 ]

jesse ravind.kumar I think we should backport this change to 4.2, since it would be nice to get this simple fix available in the most recent stable branch. I'm less sure about 4.0. I'll request backport to 4.0 and 4.2, and the query team will discuss during our next triage meeting.

Comment by A. Jesse Jiryu Davis [ 20/Aug/19 ]

david.storch do you have an opinion about backporting?

Comment by Ravind Kumar (Inactive) [ 20/Aug/19 ]

jesse is there any intent to backport these changes? Just curious.

Comment by Githook User [ 20/Aug/19 ]

Author:

{'username': 'ajdavis', 'email': 'jesse@mongodb.com', 'name': 'A. Jesse Jiryu Davis'}

Message: SERVER-37690 Return 0 from countDocuments for empty collection
Branch: master
https://github.com/mongodb/mongo/commit/d648da63525c58f7a842e0366066841dcecaabb5

Comment by Miha Filej [ 28/Jan/19 ]

I can confirm this behaviour on 4.0.5.

Generated at Thu Feb 08 04:46:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.