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

Posted

技术标签:

【中文标题】MongoDB $geoIntersects 不返回特定城市的任何数据【英文标题】:MongoDB $geoIntersects doesn't return any data for particular city 【发布时间】:2018-06-22 20:05:20 【问题描述】:

我从谷歌地图中手动获取钦奈和班加罗尔城市的坐标并插入到我的数据库中。对于班加罗尔市,$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"
    

我用来查找 chennai 的查询

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

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

【问题讨论】:

【参考方案1】:

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

为了解决这个问题,您可以在数据库中重新排序不正确的coordinatesChennai 的有效文档可能如下所示:

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。在这种情况下,您的查询工作正常。

【讨论】:

以上是关于MongoDB $geoIntersects 不返回特定城市的任何数据的主要内容,如果未能解决你的问题,请参考以下文章

如何在 MongoDB 中正确使用 $geoIntersects?

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

mongodb - $geoIntersects 按重叠百分比排序

地理空间查询是不是适用于数组? ($geoWithin, $geoIntersects)

$geoIntersects 不像预期的那样工作

如何在 $geoIntersects 查询中使用当前文档字段之一作为坐标