Geo-Spatial Index:
A geospatial index that can be used to process geo-location-based queries.
Point: Used to specify the exact location, we take restaurants as an example:
Db.restaurants.insert ({name: "Citi", loc: {type: "point", Coordinates: [52.37, 5.21]}})
Db.restaurants.insert ({name: "SAP", loc: {type: "point", Coordinates: [51.91, 4.41]}})
Db.restaurants.insert ({name: "IBM", loc: {type: "point", Coordinates: [52.36, 4.89]}})
Create 2dsphere Index: (longitude default range is 180 to 180, we change to-500 to 500)
Db.restaurants.ensureIndex ({loc: "2dsphere"},{min: -500, max:500})
Search for restaurants within 40000 meters of the maximum distance from the specified location:
Db.restaurants.find ({loc: {$geoNear: {$geometry: {type: ' point ', coordinates:[52.33,5.51]}, $maxDistance: 40000}})
Review the execution plan and discover that the 2dsphere index is used:
Db.restaurants.find ({loc: {$geoNear: {$geometry: {type: "point", coordinates:[52.33,5.51]}, $maxDistance: 40000}}). Explain (true)
By default, running a query with the Find () function is sufficient, but MongoDB also provides the Geonear function, which also provides the distance from the specified point to each record in the query results, as well as some additional diagnostic information.
Db.runcommand ({geonear: "Restaurants", near: {type: ' point ', coordinates: [52.33, 5.51]}, spherical:true})
There are many geospatial types and functions, but similar to the use of points, here is not one by one for example, if you use it in the work, you can check the official website.
Getting started with the geo-spatial index of MongoDB Learning notes