Articles → MONGODB → $Centersphere Operator In Mongodb

$Centersphere Operator In Mongodb






Purpose






Picture showing how the distance is measured from the radius in a sphere





Syntax


{
  $geoWithin: {
    $centerSphere: [ [ <x>, <y> ], <radius> ]
  }
}




Distance/6378.1





Creating The 2Dsphere Index




db.locations.createIndex({ location: "2dsphere" });



Example




Picture showing the sample mongodb collection




db.locations.find({
  location: {
    $geoWithin: {
      $centerSphere: [ [ 77.2441, 28.6189 ], 4 / 6378.1 ]
    }
  }
});



Output


Picture showing the output of $centersphere operator in mongodb



Posted By  -  Karan Gupta
 
Posted On  -  Friday, May 31, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250