简体   繁体   中英

MongoDB $geoIntersects doesn't return any data for particular city

I got coordinates manually from google maps for Chennai and Bangalore cities and inserted into my DB. For Bangalore city, $geoIntersect works perfectly. However, for some reason for Chennai city, it doesn't show up. Am not sure where/what am I missing. Attaching my code below

Bangalore City Details

{
    "_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"
    }
 }

Query i used

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

Chennai City Details

{
    "_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"
    }
}

Query i used to find chennai

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

For Chennai query, I get No records found. Have I inserted coordinates in the wrong manner or any other issue? Any help would be appreciated. Thanks

Bangalore City coordinates are represented by valid, closed polygon and that's why MongoDB query works. In Chennai case the order of coordinates is incorrect. You can easily check it here :

在此输入图像描述

So to fix that you can reorder incorrect coordinates in the database. Valid document for Chennai could look like this:

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"
        }
    })

Which can be previewed here . In that case your query works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM