MongoDb: $geoIntersects for multiple input coordinates

Viewed 549

I have a list of coordinates for each of them I need to perform $geoIntersects query. For one coordinate the query looks like this:

db.polygons.find({ 
    geometry: { 
        $geoIntersects: {
            $geometry: {
                type: "Point", coordinates: [24.053640, 49.812427]
            } 
        } 
    } 
})

The problem is that there is a list of input coordinates and for each of them I need to find a polygon. The only way I've found so far is to iterate over them in application code and execute query N times.

Is it possible to do it with one MongoDB query? If not, any ideas on how this can optimized are appreciated.

1 Answers

One possible solution is replacing the Point geometry type with the MultiPoint one, and passing in the the list of input coordinates as an array.

db.polygons.find({ 
geometry: { 
    $geoIntersects: {
        $geometry: {
            type: "MultiPoint", coordinates: [[24.053640, 49.812427],[...]...] 
        } 
    } 
} 
})
Related