fdezjose
fdezjose

Reputation: 617

MongoDB - Geospatial distance problem

I have a 2d geospatial index on an attribute "location". I'm trying to query certain entities within a given range of a latitude/longitude location. If I use the geoNear command I get correct results:

distances = db.runCommand({ geoNear: "Places", near: [40.423776,-3.711534], spherical: true, maxDistance: 10/6378}).results

In outputs all the places within 10 km of the given coordinates.

But if I execute the following query I get no results: db.Places.find({location: { $near [40.423776,-3.711534], $maxDistance: 10/6378, $nearSphere: true }})

Am I doing something wrong?

Upvotes: 0

Views: 919

Answers (1)

RameshVel
RameshVel

Reputation: 65877

$nearSphere should be used like this

db.Places.find({location:{$nearSphere:[[40.423776,-3.711534],10/6378]}}).count()

Hope it helps

Upvotes: 5

Related Questions