【问题标题】:MongoDB - $geoWithin query using $polygon or $geometry gives different resultMongoDB - 使用 $polygon 或 $geometry 的 $geoWithin 查询给出不同的结果
【发布时间】:2016-10-07 04:54:17
【问题描述】:

使用 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 ] 
                ]                 
        } 
    } 
})

【问题讨论】:

  • 这有点奇怪...,尤其是 query.explain("executionStats") 表明第一个查询正在使用索引(如果已创建)而另一个没有...

标签: mongodb


【解决方案1】:

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

当使用 $polygon 时,会计算平面几何,并且该点落在您描述的多边形内。但是当您使用 GeoJSON 时,使用的是球面几何,并且该点落在几何之外

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

【讨论】:

    猜你喜欢
    • 2019-04-02
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多