Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in $geoWithin and $geoIntersects operators?

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.

like image 523
Dev Avatar asked Apr 28 '15 19:04

Dev


1 Answers

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
like image 105
wdberkeley Avatar answered Sep 28 '22 01:09

wdberkeley