简体   繁体   中英

How to find all mongoDB documents based on geolocation

I'm using the react geosuggest package to save locations of my events in MongoDB. The package takes data from Google places and each time I store it, it gets stored like this:

"_id": "vvagbSbEginyrQGK8",
"name": "test3",
"googleLocation": {
    "label": "Testaccio, Roma, Italia",
    "placeId": "ChIJE47T5i5gLxMRhiCxCUAFq4Q",
    "isFixture": false,
    "gmaps": {
        "address_components": [
            {
                "long_name": "Monte Testaccio",
                "short_name": "Monte Testaccio",
                "types": [
                    "establishment",
                    "natural_feature"
                ]
            },
            {
                "long_name": "Rome",
                "short_name": "Rome",
                "types": [
                    "locality",
                    "political"
                ]
            },
            {
                "long_name": "Rome",
                "short_name": "Rome",
                "types": [
                    "administrative_area_level_3",
                    "political"
                ]
            },
            {
                "long_name": "Metropolitan City of Rome",
                "short_name": "RM",
                "types": [
                    "administrative_area_level_2",
                    "political"
                ]
            },
            {
                "long_name": "Lazio",
                "short_name": "Lazio",
                "types": [
                    "administrative_area_level_1",
                    "political"
                ]
            },
            {
                "long_name": "Italia",
                "short_name": "IT",
                "types": [
                    "country",
                    "political"
                ]
            },
            {
                "long_name": "00153",
                "short_name": "00153",
                "types": [
                    "postal_code"
                ]
            }
        ],
        "formatted_address": "Monte Testaccio, 00153 Rome, Italia",
        "geometry": {
            "location": {},
            "location_type": "APPROXIMATE",
            "viewport": {
                "f": {
                    "b": 41.87460301970851,
                    "f": 41.87730098029151
                },
                "b": {
                    "b": 12.474345019708494,
                    "f": 12.477042980291571
                }
            }
        },
        "place_id": "ChIJE47T5i5gLxMRhiCxCUAFq4Q",
        "types": [
            "establishment",
            "natural_feature"
        ]
    },
    "location": {
        "lat": 41.87595200000001,
        "lng": 12.475693999999976
    }
}

I've tried a lot of different queries with the mongodb near query, but I can't figure it out. Anyone know about a query that will for example find all documents within 10000 meter based on longitude and latitude that I send in.

According to the current version of mongodb, it has Gospatial query operators and you can achieve what you need after modifying the collection which contains the places as following:

First you should create "2dsphere" index for the collection, let's name it "places" as following:

db.places.createIndex( { loc : "2dsphere" } );
/*
the document should contain "loc" property as following:
{
  loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
  //+ the other needed properties.
}
*/

Then for a specific origin and distance range, you can apply the following query:

db.places.find(
{
   loc:
   { $near :
     {
        $geometry: { type: "Point",  coordinates: [ -73.9667, 40.78 ] 
        //the origin point.
        },
        $minDistance: 1000, //in meter
        $maxDistance: 5000  //in meter
      }
   }
}
)

Note that the coordinates ordered as following [long,lat].

for more information you can check the mongodb document in the following link:

  1. 2Dshpere indexes
  2. Geospatial operators

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