[SERVER-8514] $in count query with null array does not yield error on empty collection Created: 11/Feb/13  Updated: 24/Nov/14  Resolved: 14/Nov/14

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.2.0
Fix Version/s: 2.8.0-rc1

Type: Bug Priority: Major - P3
Reporter: Daniel Doubrovkine Assignee: Unassigned
Resolution: Done Votes: 0
Labels: pull-request, query_triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OSX


Attachments: File server8514.js    
Operating System: ALL
Participants:

 Description   

A count on a collection that exists yields an error on an invalid query. All is good.

> db.foos.insert(

{ bars: [ 1, 2 ]}

)
> db.foos.find({ bars : { $in : [ 1 ]}}).count()
1
> db.foos.find({ bars : { $in : null }}).count()
Mon Feb 11 16:45:53 uncaught exception: count failed:

{ "errmsg" : "exception: invalid query", "code" : 12580, "ok" : 0 }

A count on a collection that doesn't exist yields 0, which is, IMHO, incorrect.

> db.invalid.find({ bars : { $in : null }}).count()
0

This is an odd optimization, I would expect them to fail the same way.



 Comments   
Comment by Githook User [ 14/Nov/14 ]

Author:

{u'username': u'Michael-karn-ivanov', u'name': u'Michael Ivanov', u'email': u'michael.karn.ivanov@gmail.com'}

Message: SERVER-8514 return error on invalid count query on empty collection

Closes #783

Signed-off-by: Benety Goh <benety@mongodb.com>
Branch: master
https://github.com/mongodb/mongo/commit/a57241b5823968515be18614b1358e4bd7db5acc

Comment by David Storch [ 18/Sep/14 ]

Hi,

Thanks very much for submitting Pull Request #780! We have reviewed this change, and unfortunately concluded that it is not the correct approach for addressing this issue. When a namespace does not exist, a count operation should

  • return 0 if the query is valid, or
  • fail if the query is invalid.

The correct behavior would look something like this:

> t = db.<collection>;
> t.drop();
> t.count({a: {$in: [2]}});
0
> t.count({a: {$in: null}}); // returns an error

The bug is that all versions of MongoDB since 2.2.0 return 0 for the latter case:

> t.count({a: {$in: [2]}});
0
> t.count({a: {$in: null}});
0

With this change, the bug is fixed, but valid count queries against a non-existent collection would also fail. Hopefully this explanation makes sense, and feel free to reach out with any further questions.

Best,
Dave

Comment by Aaron Staple [ 07/Mar/13 ]

Confirmed that this is still present in 2.2.3.

Generated at Thu Feb 08 03:17:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.