If so, which one?
How do we know?
I have 3 indexes in a query. Actually only the first 2 should be useful
{
"v": NumberInt(1),
"key": {
"LongitudeLatitude": "2d",
"Prominent"▼: NumberInt(-1),
"indexContents": NumberInt(1)
},
"ns": "database.tablebusiness",
"name": "LongLat_Prominent_indexContents",
"dropDups": false,
"background": false
}
{
"v": NumberInt(1),
"key": {
"LongitudeLatitude": "2d",
"Prominent": NumberInt(-1)
},
"ns": "database.tablebusiness",
"name": "LongLat_Prominent",
"dropDups": false,
"background": false
}
This one is not necessary:
{
"v": NumberInt(1),
"key": {
"indexContents": NumberInt(1)
},
"ns": "database.tablebusiness",
"name": "indexContents",
"dropDups": false,
"background": false
}
Now I do this query
(the query is from PhP so I'll just paste what I see in mongodb log and mongovue
database.tablebusiness query: { LongitudeLatitude: { $nearSphere: [ 106.772835, -6.186753 ], $maxDistance: 0.04498373205078301 }, Prominent: { $gte: 15 }, indexContents: { $all: [ /^aru/, /^op/ ] } } ntoreturn:20 ntoskip:80 nscanned:100 nreturned:20 reslen:1147 1927ms
Now copying { LongitudeLatitude: { $nearSphere: [ 106.772835, -6.186753 ], $maxDistance: 0.04498373205078301 }, Prominent: { $gte: 15 }, indexContents: { $all: [ /^aru/, /^op/ ] } } into mongovue find command gets me the real database command
db.tablebusiness.find({ "LongitudeLatitude" : { "$nearSphere" : [106.772835, -6.186753], "$maxDistance" : 0.044983732050783008 }, "Prominent" : { "$gte" : 15 }, "indexContents" : { "$all" : [/^aru/, /^op/] } }).limit(50);
Which is good enough (except that the limit change a little bit). Doing explain in mongo.exe I got
db.tablebusiness.find({ "LongitudeLatitude" : { "$nearSphere" : [106.772835, -6.186753], "$maxDistance" : 0.044983732050783008 }, "Prominent" : { "$gte" : 15 }, "indexContents" : { "$all" : [/^aru/, /^op/] } }).limit(50).explain();
{
"cursor" : "GeoSearchCursor",
"nscanned" : 50,
"nscannedObjects" : 50,
"n" : 50,
"millis" : 3291,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
}
}
Now based on that, what index is used? Does mongodb use any index at all? Any tips on how to improve this query will also be appreciated.