Rex Adrivan
Rex Adrivan

Reputation: 1009

mongodb filter all longitude and latitude within radius

I have a mongodb database with longitude and latitude. What I want is return all data with longitude and latitude within 1KM radius or higher.

{
   user:rex,
   longitude: 24,
   latitude: 20

}

Upvotes: 0

Views: 2385

Answers (1)

Moshi
Moshi

Reputation: 1433

To filter collection in MongoDB with radius at first create the geospatial index.

If your collection name is user then

db.user.ensureIndex({loc:"2d"})

You need to convert the radius value to km. By default mongodb $near accepts $maxDistance as radius.

Now you can search within 1km radius

 db.user.find({ loc : { $near : [yourLat,yourLon] , $maxDistance : 100/111.12 } } )

for more MongoDb version 2.2 doc

Upvotes: 2

Related Questions