MongoDB - 使用 $polygon 或 $geometry 的 $geoWithin 查询给出不同的结果

标签 mongodb

使用 MongoDB 3.2 我正在尝试对点集合使用 2dsphere 查询。

假设我有一个集合 cust_5_abcd,在 the_geom 字段上有一个 2dsphere 索引。

在集合中添加几何图形:

db.cust_5_abcd.insert({
"chps0" : "Texte d'une ligne",
"the_geom" : {
    "type" : "Point",
    "coordinates" : [ 
        1.032715, 
        40.380028
    ]
}})

现在我正在尝试使用 $geoWithin 查询此点,以获取特定多边形内的所有数据。如果我将 $geometry 与 GeoJSON 定义一起使用,或者与 $polygon 和严格坐标一起使用,这就是我得到不同结果的地方。也许文档中的某些内容丢失或我误解了。

$geometry 没有结果:

db.cust_5_abcd.find(  { the_geom: 
 { $geoWithin: 
        { $geometry: 
            {       
                "type": "Polygon", 
                "coordinates": [ 
                    [ 
                        [ -16.237793, 40.162083 ], 
                        [ -16.237793, 51.835778 ], 
                        [ -13.776855, 51.835778 ], 
                        [ -13.776855, 41.426253 ], 
                        [ 14.765625, 41.426253 ], 
                        [ 14.765625, 40.162083 ], 
                        [ -16.237793, 40.162083 ] 
                ] 
                ]
            } 
        } 
    } 
})

使用 $polygon 返回我的点:

db.cust_5_abcd.find( { the_geom: 
 { $geoWithin: 
        { $polygon:
                    [ 
                        [ -16.237793, 40.162083 ], 
                        [ -16.237793, 51.835778 ], 
                        [ -13.776855, 51.835778 ], 
                        [ -13.776855, 41.426253 ], 
                        [ 14.765625, 41.426253 ], 
                        [ 14.765625, 40.162083 ], 
                        [ -16.237793, 40.162083 ] 
                ]                 
        } 
    } 
})

最佳答案

从 mongodb 人那里得到答案:https://jira.mongodb.org/browse/SERVER-24549?focusedCommentId=1293398&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-1293398

When using $polygon a planar geometry is calculated, and the point falls inside the polygon you describe. But when you use GeoJSON a spherical geometry is used, and the point falls outside the geometry

现在的问题是我不能在 $geoIntersect 上使用 $polygon 例如或与其他几何形状然后是多边形。

关于MongoDB - 使用 $polygon 或 $geometry 的 $geoWithin 查询给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37676794/

相关文章:

java - 使用 spring data MongoRepository 更新查询的自定义方法

mongodb - 使用 mongodb 中的聚合获取所有具有最大值的文档

oracle - Oracle Data Integrator 有没有办法从 MongoDB 中提取数据

javascript - 在 MongoDB 中查找包含字符串 ID 数组的文档

mongodb - Mongoose 更新: Increment a counter and reset to 0 on a new date

node.js - Mongoose 删除带有引用的文档

mongodb - 有什么理由不使用 OrientDB 吗?

javascript - 使用 cursor.forEach() 时什么时候调用 db.close()?

javascript - 如何修复ejs中的 "Unexpected token < "错误

database - 如何在 Go 中重用 MongoDB 连接