[SERVER-11517] Mongo index on implicit/explicit $and not consistent Created: 01/Nov/13  Updated: 11/Jul/16  Resolved: 01/Nov/13

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.4.6
Fix Version/s: 2.5.3

Type: Bug Priority: Minor - P4
Reporter: George Narroway Assignee: hari.khalsa@10gen.com
Resolution: Done Votes: 0
Labels: query_triage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 64bit, 2.4.6.


Attachments: File server11517.js    
Operating System: ALL
Steps To Reproduce:

// Populate
db.test.insert({_id:1,Fields:{K1:123,K2:456}})
db.test.insert({_id:2,Fields:{K1:456,K2:123}})
 
// Index on fields of subdocument
db.test.ensureIndex({"Fields.K1": 1})
db.test.ensureIndex({"Fields.K2": 1})...
 
// Execute some queries
db.test.find({_id: {$lt: 20}, "$or": [{"Fields.K1": 123}, {"Fields.K2": 123}]}).explain()
db.test.find({$and: [{_id: {$lt: 20}}, {"$or": [{"Fields.K1": 123}, {"Fields.K2": 123}]}]}).explain()

Participants:

 Description   

Mongo allegedly uses an implicit AND when quering comma-separated fields of the form:

coll.find({k1:v1, k2:v2})

Thus the above should be equivalent in all senses to:

coll.find({$and: [{k1:v1}, {k2:v2}]})

However, including the explicit $and alters the indexing behaviour of the search. Using the below steps, the explicit $and uses only the _id index, whereas the implicit $and utilises other indexes. Trying to force the explicit $and to use the index that the implicit version is using (with .hint()) does not work.



 Comments   
Comment by Benety Goh [ 01/Nov/13 ]

This behavior is now consistent as a result of the recent query framework enhancements.

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