12

I can't seem to find this information. But using a geo search you can specify $maxDistance but for the life of me, I can't find if that is miles, kilometers, etc.

Here is a sample query I'm using:

db.hotels.find( { location: { $near : [18.74255, 99.03523], $maxDistance : 0.05 } } ).count()

Returns 15 (from my hotels database).

Thanks for any information.

cbmeeks
  • 11,248
  • 22
  • 85
  • 136

2 Answers2

22

Unless you're doing a spherical query (like $nearSphere), MongoDB doesn't actually care about units, it just treats your locations as Cartesian coordinates.

So, the calculated distance is going to be in whatever your input units were. Since your $near point looks like standard lat/long, the $maxDistance in this case will be in units of degrees.

John Flatness
  • 32,469
  • 5
  • 79
  • 81
  • What is units of degrees? I am doing the almost exact query as cbmeeks. What maxdistance should I set if I need results from within 1 km? – Jonathan Clark Oct 20 '11 at 14:40
  • 2
    It's not exact, but for a rough conversion, you can divide your desired distance in kilometers by 111. (For miles, you would divide by about 69.) – John Flatness Oct 20 '11 at 17:35
  • 11
    Just to be more exact since I just researched for this and this can clearly help future visitors, the actual formula is : 1° latitude = 69.047 miles = 111.12 kilometers – Dominic Goulet Jun 29 '12 at 16:29
  • 1
    It is not so simple for longitude though, since the conversion factor changes with latitude. – Rasmus Storjohann Mar 04 '14 at 21:38
3

If you are using $near,

$maxDistance = your distance in Kms / 111 

and if you use $nearSphere,

$maxDistance = your distance in kms/ 6378

where 6378 is the earths radius in kms

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
NIJESH ku
  • 409
  • 2
  • 7