Articles → MONGODB → $Geonear Aggregation Stage In Mongodb
$Geonear Aggregation Stage In Mongodb
Purpose
Syntax
{
$geoNear: {
near: { type: "Point", coordinates: [ <longitude> , <latitude> ] },
distanceField: "<outputField>",
maxDistance: <distance in meters>
}
}
Creating The 2Dsphere Index
db.locations.createIndex({ location: "2dsphere" });
Example
db.locations.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ 77.2295, 28.6129 ] },
distanceField: "distance",
maxDistance: 2000
}
}
])
Output