I have several geo queries which look similar to the below example. They are not only a point in Poly query but have other aspects to it.
Query:
{
"point": {
"$within": {
"$polygon": [
[
41.878335,
-87.627319999999997
],
[
41.877602000000003,
-87.627319999999997
],
[
41.879672999999997,
-87.627031000000002
],
[
41.878723999999998,
-87.627204000000006
],
[
41.878507999999997,
-87.627262000000002
],
[
41.878335,
-87.627262000000002
]
]
}
},
"MYLISTING": {
"$in": [
"All"
]
},
"SUPPORT": {
"$in": [
"Y"
]
},
"TYPE": {
"$in": [
"Food"
]
}
}
To help make these quicker I added each of the other items to an index making it:
{ "v" : 1,
"key" : {
"point" : "2d",
"MYLISTING" : 1,
"SUPPORT" : 1,
"TYPE" : 1
},
"ns" : "mydb.collection",
"name" : "point_2d_MYLISTING_1_SUPPORT_1_TYPE_1"
}
However when doing an explain on this, it never seems to be using this index, it’s simply using the other index I have which is only the point:2d index initially created.
is there some mis-understandign I’m having here?
You can only have one geo index – now you have two, where one of the parts is part of your compound index. The limitation is mentioned in the documentation: http://api.mongodb.org/wiki/current/Geospatial%20Indexing.html#GeospatialIndexing-CreatingtheIndex
The solution would be to simply drop the index that you have on just the spatial field:
When you then run your query, with explain(), you see which index is now used again – and in this case, that should be the compound index. Also make sure, that the geo spatial field is the first part of your compound index as described here: http://api.mongodb.org/wiki/current/Geospatial%20Indexing.html#GeospatialIndexing-CompoundIndexes