OPTIONS

# Calculate Distance Using Spherical Geometry¶

Note

While basic queries using spherical distance are supported by the 2d index, consider moving to a 2dsphere index if your data is primarily longitude and latitude.

The 2d index supports queries that calculate distances on a Euclidean plane (flat surface). The index also supports the following query operators and command that calculate distances using spherical geometry:

Important

These three queries use radians for distance. Other query types do not.

For spherical query operators to function properly, you must convert distances to radians, and convert from radians to the distances units used by your application.

To convert:

• distance to radians: divide the distance by the radius of the sphere (e.g. the Earth) in the same units as the distance measurement.
• radians to distance: multiply the radian measure by the radius of the sphere (e.g. the Earth) in the units system that you want to convert the distance to.

The radius of the Earth is approximately 3,959 miles or 6,371 kilometers.

The following query would return documents from the places collection within the circle described by the center [ -74, 40.74 ] with a radius of 100 miles:

```db.places.find( { loc: { \$geoWithin: { \$centerSphere: [ [ -74, 40.74 ] ,
100 / 3959 ] } } } )
```

You may also use the distanceMultiplier option to the geoNear to convert radians in the mongod process, rather than in your application code. See distance multiplier.

The following spherical query, returns all documents in the collection places within 100 miles from the point [ -74, 40.74 ].

```db.runCommand( { geoNear: "places",
near: [ -74, 40.74 ],
spherical: true
}  )
```

The output of the above command would be:

```{
// [ ... ]
"results" : [
{
"dis" : 0.01853688938212826,
"obj" : {
"_id" : ObjectId( ... )
"loc" : [
-73,
40
]
}
}
],
"stats" : {
// [ ... ]
"avgDistance" : 0.01853688938212826,
"maxDistance" : 0.01853714811400047
},
"ok" : 1
}
```

Warning

Spherical queries that wrap around the poles or at the transition from -180 to 180 longitude raise an error.

Note

While the default Earth-like bounds for geospatial indexes are between -180 inclusive, and 180, valid values for latitude are between -90 and 90.

## Distance Multiplier¶

The distanceMultiplier option of the geoNear command returns distances only after multiplying the results by an assigned value. This allows MongoDB to return converted values, and removes the requirement to convert units in application logic.

Using distanceMultiplier in spherical queries provides results from the geoNear command that do not need radian-to-distance conversion. The following example uses distanceMultiplier in the geoNear command with a spherical example:

```db.runCommand( { geoNear: "places",
near: [ -74, 40.74 ],
spherical: true,
distanceMultiplier: 3959
}  )
```

The output of the above operation would resemble the following:

```{
// [ ... ]
"results" : [
{
"dis" : 73.46525170413567,
"obj" : {
"_id" : ObjectId( ... )
"loc" : [
-73,
40
]
}
}
],
"stats" : {
// [ ... ]
"avgDistance" : 0.01853688938212826,
"maxDistance" : 0.01853714811400047
},
"ok" : 1
}
```