MongoDB can store geospatial information and perform queries on it. In order to do so, you should store information as GeoJSON objects.
The format of a GeoJSON point is as follows:
{
"type": "Point",
"coordinates": [ -73.856077, 40.848447 ]
}
The two numbers are the longitude and latitude. A third optional number may be included to indicate the altitude.
This is an example of how to define a location
property with this format in a
Mongoose schema:
const geolocatedSchema = new Schema({
location: {
type: {
type: String,
required: true,
enum: [ 'Point' ]
},
coordinates: {
type: [ Number ],
required: true,
validate: {
validator: validateGeoJsonCoordinates,
message: '{VALUE} is not a valid longitude/latitude(/altitude) coordinates array'
}
}
}
});
// Create a geospatial index on the location property.
geolocatedSchema.index({ location: '2dsphere' });
// Validate a GeoJSON coordinates array (longitude, latitude and optional altitude).
function validateGeoJsonCoordinates(value) {
return Array.isArray(value) && value.length >= 2 && value.length <= 3 && isLongitude(value[0]) && isLatitude(value[1]);
}
function isLatitude(value) {
return value >= -90 && value <= 90;
}
function isLongitude(value) {
return value >= -180 && value <= 180;
}