Show
Query:
{
"a" : {
"$gt" : 5
},
"b" : {
"$lt" : 500
},
"$or" : [
{
"startLoc" : {
"$geoWithin" : {
"$geometry" : {
"type" : "MultiPolygon" ,
"coordinates" : [[[[-1,-1],[2,-2],[2,2],[-2,2],[-1,-1]]]]
}
}
}
},
{
"endLoc" : {
"$geoWithin" : {
"$geometry" : {
"type" : "MultiPolygon" ,
"coordinates" : [[[[-1,-1],[2,-2],[2,2],[-2,2],[-1,-1]]]]
}
}
}
}
]
}
Insert a document
db.docs.insert({a: 100, b: 100, startLoc: {type: "Point" , coordinates: [0,0]}, endLoc: {type: "Point" , coordinates: [1,1]}})
When the indexes are not compound indexes, the query can use the indexes:
db.docs.ensureIndex({startLoc: "2dsphere" })
db.docs.ensureIndex({endLoc: "2dsphere" })
db.docs.getIndexKeys()
db.docs.find({a: {$gt: 5},b: {$lt: 500}, startLoc:{ "$geoWithin" : { "$geometry" : { "type" : "MultiPolygon" , "coordinates" : [[[[-1,-1],[2,-2],[2,2],[-2,2],[-1,-1]]]]}}} }).explain( true )
When the indexes are compound 2dsphere indexes, the query cannot use the indexes:
db.docs.dropIndexes()
db.docs.ensureIndex({startLoc: "2dsphere" , a: 1, b: 1})
db.docs.ensureIndex({endLoc: "2dsphere" , a: 1, b: 1})
db.docs.getIndexKeys()
db.docs.find({a: {$gt: 5},b: {$lt: 500}, startLoc:{ "$geoWithin" : { "$geometry" : { "type" : "MultiPolygon" , "coordinates" : [[[[-1,-1],[2,-2],[2,2],[-2,2],[-1,-1]]]]}}} }).explain( true )