indexing - mongodb index range find -
if have these docs:
{"name": 1, "score": 2} {"name": 1, "score": 4} {"name": 2, "score": 2} {"name": 2, "score": 4}
if ensure index:
db.test.ensureindex({"name":1, "score":1})
then try find():
db.test.find({"score": 4})
i use explain(), , found query can not use index , scan 4 docs.
i wonder why scans docs?
you know, if enum "name" 's value (1 , 2):
db.test.find({"$or":["name":1, "name":2], "score":4})
it can use index , scanned 2 docs.
why mongodb can not thing me?
the reason index not being used because $or exclusion operator, , means more or less scans in order determine not match.
what want use $in. reduce match only values contained in set.
db.test.find({"$in":["name":1, "name":2], "score":4})
as inclusive match can applied index without doing full scan.
Comments
Post a Comment