简体   繁体   中英

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.

From $geoIntersects :

Selects documents whose geospatial data intersects with a specified GeoJSON object; ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM