gpt4 book ai didi

mongodb - $geoWithin 和 $geoIntersects 运算符的区别?

转载 作者:可可西里 更新时间:2023-11-01 09:20:16 24 4
gpt4 key购买 nike

mongoDB 中的$geoWithin$geoIntersects 运算符有什么区别?

如果我正在寻找坐标(使用默认坐标引用系统),$geoWithin$geoIntersects 将返回相同的结果。

如有错误请指正。

如果能提供任何用于理解差异的简单用例,我们将不胜感激。

最佳答案

来自 $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.

来自 $geoWithin :

Selects documents with geospatial data that exists entirely within a specified shape.

举个例子:

> 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

关于mongodb - $geoWithin 和 $geoIntersects 运算符的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29928678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com