$geoWithin $box 错误?

Eri*_*rik 0 geospatial mongodb mongodb-query

我的 MongoDB 数据库中有以下文档:

{
    "_id": {
        "$oid": "55002a7916157e6021de5708"
    },
    "city": "Wellington New Zealand",
    "location": [
        174.77623600000004,
        -41.2864603
    ]
}
Run Code Online (Sandbox Code Playgroud)

当我尝试通过以下地理空间查询找到它时,我没有得到它:

db.collection.find({
    "location": {
        "$geoWithin": {
            "$box": [
                [165.8694369, -52.61941849999999],
                [-175.831536, -29.2313419]
            ]
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

您能解释一下为什么上面的查询不起作用吗?

Nei*_*unn 6

这些坐标无效,因为“右上角”的经度与应有的经度相反:

db.collection.find({
    "location": {
        "$geoWithin": {
            "$box": [
                [165.8694369, -52.61941849999999],
                [175.831536, -29.2313419]
            ]
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

这将正确返回所需的位置。我建议您在测试功能时使用http://geojsonlint.com/之类的东西。

你的“盒子”看起来像这样:

在此输入图像描述

当然,它覆盖了“除了”所需区域之外的纬度之间的所有地球。这就是它失败的原因。