mongodb - Index prefix doesn't work if a compound index contains 2dsphere index -



mongodb - Index prefix doesn't work if a compound index contains 2dsphere index -

> db.test.ensureindex({x: 1, location: '2dsphere'}) { "createdcollectionautomatically" : false, "numindexesbefore" : 1, "numindexesafter" : 2, "ok" : 1 } > db.test.find({x: 0}).explain() { "cursor" : "basiccursor", "ismultikey" : false, "n" : 1, "nscannedobjects" : 100009, "nscanned" : 100009, "nscannedobjectsallplans" : 100009, "nscannedallplans" : 100009, "scanandorder" : false, "indexonly" : false, "nyields" : 781, "nchunkskips" : 0, "millis" : 40, "server" : "hackintosh:27017", "filterset" : false }

mongodb version: 2.6.2

i created compound index on x , location, when queried on x, why didn't work?

i'm guessing document doesn't have location key or location null query not utilize index unless explicitly hinted.

that's because 2dsphere indexes in mongodb 2.6 sparse. can utilize hint() explicitly specify index, not find documents without location field because not added index.

from docs:

if document lacks 2dsphere index field (or field null or empty array), mongodb not add together entry document 2dsphere index. inserts, mongodb inserts document not add together 2dsphere index.

for compound index includes 2dsphere index key along keys of other types, 2dsphere index field determines whether index references document.

if have find documents it's x field whether loc set or not suggest adding separate index (non-sparse index) field.

edit

i did additional testing. in case seems mongodb default using basiccursor, unless explicitly specify index hint. sammaye said, it's known quirk.

mongodb geospatial mongodb-indexes 2dsphere

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -