[SERVER-393] $exists queries should use index Created: 28/Oct/09  Updated: 12/Jul/16  Resolved: 06/Apr/11

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 1.9.0

Type: Improvement Priority: Minor - P4
Reporter: Mathias Stearn Assignee: Aaron Staple
Resolution: Done Votes: 19
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to SERVER-2905 Scan and order sort should treat miss... Closed
is related to SERVER-429 btree change (Sorting and other) mast... Closed
Participants:

 Comments   
Comment by auto [ 05/Apr/11 ]

Author:

{u'login': u'astaple', u'name': u'Aaron', u'email': u'aaron@10gen.com'}

Message: SERVER-393 SERVER-2902 additional tests
Branch: master
https://github.com/mongodb/mongo/commit/4c981f252999738777e9c6bf1743aea22941861c

Comment by auto [ 05/Apr/11 ]

Author:

{u'login': u'astaple', u'name': u'Aaron', u'email': u'aaron@10gen.com'}

Message: SERVER-393 Use index bounds for exists:false
Branch: master
https://github.com/mongodb/mongo/commit/a7360cc67fffc5951f1f5e898f90e2fc3575e396

Comment by auto [ 05/Apr/11 ]

Author:

{u'login': u'astaple', u'name': u'Aaron', u'email': u'aaron@10gen.com'}

Message: SERVER-393 Allow rejecting document using non exists indexed fields when an exists constraint is present
Branch: master
https://github.com/mongodb/mongo/commit/f97863227992f5e25ebe0882875d54cc5cc5937a

Comment by Scott Hernandez (Inactive) [ 03/Jun/10 ]

(If you, like me, are searching for the issue about why $exists fails in the cases below, this is the issue to watch. Taken from a duplicate issue.)

> db.people.save(

{ fname: "BOB", lname: "SMITH" }

)
> db.people.find(

{ lname: "SMITH" }

)

{ "_id" : ObjectId("4c06e2eb4138a24d33ee0fdc"), "fname" : "BOB", "lname" : "SMITH" }

> db.people.save(

{ fname: "BOB", lname: "SMITH", mname: "ROBERT", suffix: "JR" }

)
> db.people.ensureIndex(

{"lname":1,"fname":1,"mname":1,"suffix":1}

);
> db.people.find({ lname: "SMITH", mname:

{ $exists: false }

})

-Nothing Returned-

It sure seems like it is using the index and not finding the correct doc (as shown below).

Also, using null for the missing field value returns the doc even thought that is incorrect (since the index stores a null for that index value).

> db.people.find(

{ lname: "SMITH", mname: null }

)

{ "_id" : ObjectId("4c06e2eb4138a24d33ee0fdc"), "fname" : "BOB", "lname" : "SMITH" }
Comment by Mathias Stearn [ 07/Dec/09 ]

Need to meet to discuss desired sort order.

Comment by Dwight Merriman [ 18/Nov/09 ]

also:

> db.order.find().sort(

{x:1}

)

{ "_id" : ObjectId("4b045b0beda5232d46e5db6b") }

{ "_id" : ObjectId("4b045b40eda5232d46e5db6e"), "x" :

{ $minKey : 1 }

}

{ "_id" : ObjectId("4b045b15eda5232d46e5db6c"), "x" : null } { "_id" : ObjectId("4b045b1beda5232d46e5db6d"), "x" : 1 }

{ "_id" : ObjectId("4b045b42eda5232d46e5db6f"), "x" :

{ $maxKey : 1 }

}
>
>
> db.order.ensureIndex(

{x:1}

)
>
>
> db.order.find().sort(

{x:1}

)
{ "_id" : ObjectId("4b045b40eda5232d46e5db6e"), "x" :

{ $minKey : 1 }

}

{ "_id" : ObjectId("4b045b0beda5232d46e5db6b") } { "_id" : ObjectId("4b045b15eda5232d46e5db6c"), "x" : null } { "_id" : ObjectId("4b045b1beda5232d46e5db6d"), "x" : 1 }

{ "_id" : ObjectId("4b045b42eda5232d46e5db6f"), "x" :

{ $maxKey : 1 }

}

in the above, i think the order is right for the indexed case, which means this part of the problem can be fixed without an index change.

Comment by Mathias Stearn [ 28/Oct/09 ]

Pushing to 1.3.0 because fixing this will require adjusting sort order for null.

Generated at Thu Feb 08 02:53:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.