emilebaizel
emilebaizel

Reputation: 4695

Mongo spatial query $geoNear vs $near

I am doing spatial queries in Mongo. I need to find all venues within 10 miles of San Francisco. From the command line, I am getting different results when using $geoNear (works) vs $near (doesn't work). I need to use $near because I am working with Mongoose and it uses $near.

$geoNear works. I run this command:

db.runCommand({geoNear:"venues",near:[-122.418,37.775], maxDistance:10/69});

and I get results ordered by distance, within a 10 mile radius. Great.

But then I run this $near command and I get no results:

db.venues.find({loc: {$near: [-122.418,37.775] }, $maxDistance:10/69})

From what I can tell, they are the same command. Am I missing something? If I remove the $maxDistance, I do get results back and they are correctly ordered by distance. However, I also get results over 10 miles away.

db.venues.find({loc: {$near: [-122.418,37.77] }})

Upvotes: 3

Views: 9240

Answers (1)

Asya Kamsky
Asya Kamsky

Reputation: 42352

You need to have $maxDistance : X inside the {$near} JSON document - you have it outside.

Change db.venues.find({loc: {$near: [-122.418,37.775] }, $maxDistance:10/69})

to be

db.venues.find({loc: {$near: [-122.418,37.775], $maxDistance:10/69 } })

Upvotes: 11

Related Questions