MongoDB $ geoIntersects不返回特定城市的任何数据

时间:2018-06-22 20:05:20

标签: mongodb geolocation mongodb-query

我从Google地图手动获得了钦奈和班加罗尔城市的坐标,并将其插入到我的数据库中。对于班加罗尔市,$ geoIntersect可以完美地工作。但是,出于某种原因,钦奈市没有出现。不知道我在哪里/想念什么。在

下方附加我的代码

班加罗尔城市详情

{
    "_id" : ObjectId("5b2d45ef4b511713052b49f9"),
    "cityId" : 10256,
    "countryEn" : "india",
    "countryAr" : "india ar",
    "provinceEn" : "Bangalore",
    "provinceAr" : "Bangalore ar",
    "cityEn" : "Bangalore",
    "cityAr" : "Bangalore ar",
    "lat" : 12.972442,
    "lon" : 77.580643,
    "countryCode" : "+91",
    "population" : 150257,
    "boundaries" : {
    "coordinates" : [
        [
            [
                77.747618,
                13.105034
            ],
            [
                77.758604,
                13.039487
            ],
            [
                77.816283,
                12.959201
            ],
            [
                77.825896,
                12.853452
            ],
            [
                77.742125,
                12.884244
            ],
            [
                77.700926,
                12.797213
            ],
            [
                77.461973,
                12.783821
            ],
            [
                77.412535,
                12.911017
            ],
            [
                77.390562,
                13.047514
            ],
            [
                77.525145,
                13.150506
            ],
            [
                77.636381,
                13.155855
            ],
            [
                77.747618,
                13.105034
            ]
        ]
    ],
    "type" : "Polygon"
    }
 }

我使用过的查询

db.Cities_Master.find({
 boundaries: {
   $geoIntersects: {
     $geometry: { type: 'Point', coordinates: [77.5476, 13.105034] },
   },
 },
 })

钦奈城市详细信息

{
    "_id" : ObjectId("5b2d4a734b511713052b4a01"),
    "cityId" : 10255,
    "countryEn" : "india",
    "countryAr" : "india ar",
    "provinceEn" : "chennai",
    "provinceAr" : "chennai ar",
    "cityEn" : "chennai",
    "cityAr" : "chennai ar",
    "lat" : 13.067439,
    "lon" : 80.237617,
    "countryCode" : "+91",
    "population" : 150257,
    "boundaries" : {
        "coordinates" : [
        [
            [
                80.309211,
                13.257326
            ],
            [
                80.332557,
                13.241285
            ],
            [
                80.332557,
                13.249305
            ],
            [
                80.295478,
                13.091519
            ],
            [
                80.248786,
                12.838584
            ],
            [
                80.149909,
                12.880089
            ],
            [
                80.097724,
                12.936309
            ],
            [
                80.123816,
                13.14368
            ],
            [
                80.320197,
                13.199841
            ],
            [
                80.178748,
                13.2092
            ],
            [
                80.239173,
                13.215885
            ],
            [
                80.309211,
                13.257326
            ]
        ]
    ],
    "type" : "Polygon"
    }
}

我曾经找到过钦奈的查询

db.Cities_Master.find({
 boundaries: {
   $geoIntersects: {
     $geometry: { type: 'Point', coordinates: [80.309211, 13.257326] },
   },
 },
})

对于Chennai查询,我没有找到记录。我是否以错误的方式或其他问题插入了坐标?任何帮助,将不胜感激。谢谢

1 个答案:

答案 0 :(得分:2)

Bangalore City坐标由有效的闭合多边形表示,这就是MongoDB查询起作用的原因。在Chennai中,坐标顺序不正确。您可以轻松检查它here

enter image description here

因此,为了解决此问题,您可以在数据库中对不正确的coordinates重新排序。 Chennai的有效文档可能如下所示:

db.Cities_Master.save({
        "_id" : ObjectId("5b2d4a734b511713052b4a01"),
        "cityId" : 10255,
        "countryEn" : "india",
        "countryAr" : "india ar",
        "provinceEn" : "chennai",
        "provinceAr" : "chennai ar",
        "cityEn" : "chennai",
        "cityAr" : "chennai ar",
        "lat" : 13.067439,
        "lon" : 80.237617,
        "countryCode" : "+91",
        "population" : 150257,
        "boundaries" : {
            "coordinates" : [
            [
                [
                    80.309211,
                    13.257326
                ],
                [
                    80.332557,
                    13.249305
                ],
                [
                    80.332557,
                    13.241285
                ],
                [
                    80.320197,
                    13.199841
                ],
                [
                    80.295478,
                    13.091519
                ],
                [
                    80.248786,
                    12.838584
                ],
                [
                    80.149909,
                    12.880089
                ],
                [
                    80.097724,
                    12.936309
                ],
                [
                    80.123816,
                    13.14368
                ],
                [
                    80.178748,
                    13.2092
                ],
                [
                    80.239173,
                    13.215885
                ],
                [
                    80.309211,
                    13.257326
                ]
            ]
        ],
        "type" : "Polygon"
        }
    })

可以here进行预览。在这种情况下,您的查询可以正常工作。