Starting from version 2.4 of MongoDB, we have the Geospatial search feature using GeoJSON format.
Tip
You can find more information about GeoJSON at the official link: http://geojson.org/.
GeoJSON is an open source specification for formatting shapes in coordinates. It is widely used and very useful for making applications with geographical data. The format is pretty simple, and we used this format on the locations model, as you can see:
var LocationSchema = new Schema({
title: String,
coordinates: {
type: [Number],
index: '2dsphere'
},
created: {
type: Date,
default: Date.now
}
});
The highlighted code is the GeoJSON format to store coordinates.
Tip
You can read more about Geospatial query on MongoDB here: https://docs.mongodb.com/manual/reference/operator/query-geospatial/, and more geospatial indexes here: https://docs.mongodb.com/manual/applications/geospatial-indexes/.