What is the difference in $geoWithin
and $geoIntersects
operators in mongoDB?
If I am looking for coordinates (using default coordinate reference system), $geoWithin
and $geoIntersects
will return the same result.
Please correct me if I am wrong.
Any simple use case for understanding the difference would be appreciated.
From $geoIntersects:
Selects documents whose geospatial data intersects with a specified GeoJSON object; i.e. where the intersection of the data and the specified object is non-empty. This includes cases where the data and the specified object share an edge.
From $geoWithin:
Selects documents with geospatial data that exists entirely within a specified shape.
Take the following example:
> db.test.drop()
> var poly1 = { "type" : "Polygon", "coordinates" : [[[0, 0], [3, 0], [0, 3], [0, 0]]] }
> var poly2 = { "type" : "Polygon", "coordinates" : [[[1, 1], [2, 1], [1, 2], [1, 1]]] }
// poly1 is a similar triangle inside poly2
> var poly3 = { "type" : "Polygon", "coordinates" : [[[1, 0], [-2, 0], [1, 3], [1, 0]]] }
// poly3 is poly1 flipped around its "vertical" edge, then bumped over one unit, so it intersects poly1 but is not contained in it
> db.test.insert({ "loc" : poly2 })
> db.test.insert({ "loc" : poly3 })
> db.test.ensureIndex({ "loc" : "2dsphere" })
> db.test.find({ "loc" : {
"$geoIntersects" : {
"$geometry" : poly1
}
} })
// poly2 and poly3 returned
> db.test.find({ "loc" : {
"$geoWithin" : {
"$geometry" : poly1
}
} })
// poly2 returned
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With