$geoWithin和$geoIntersects运算符之间的区别是什么?

9

$geoWithin$geoIntersects在mongoDB中有什么区别?

如果我使用默认的坐标参考系统寻找坐标,$geoWithin$geoIntersects将返回相同的结果。

如果我有误,请纠正我。

欢迎提供简单的用例帮助理解它们之间的区别。

1个回答

16

来自$geoIntersects:

选择空间数据与指定的GeoJSON对象相交的文档;即,数据和指定对象的交集非空。这包括数据和指定对象共享边缘的情况。

来自$geoWithin:

选择其空间数据完全存在于指定形状内的文档。

以以下示例为例:

> 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接