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




Picture showing the sample document collection in mongodb




db.locations.aggregate([
  {
    $geoNear: {
      near: { type: "Point", coordinates: [  77.2295, 28.6129 ] },
      distanceField: "distance",
      maxDistance: 2000      
    }
  }
])



Output


Picture showing the output of $geonear aggregation stage in mongodb





Posted By  -  Karan Gupta
 
Posted On  -  Thursday, June 13, 2024

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250