Difference in $geoWithin and $geoIntersects operators?
Asked Answered
F

1

9

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.

Frink answered 28/4, 2015 at 19:55 Comment(0)
C
16

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
Celia answered 29/4, 2015 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.