# 使用球面几何计算距离¶

MongoDB在 2d 索引支持在欧几里德平面上计算距离的查询。该类索引还支持如下使用球面几何来计算距离的索引操作符和命令：

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

• 从距离到弧度: 将距离除以球面的半径（例如地球半径），两者单位一致。

• 从弧度到距离:将弧度乘以球面半径（例如地球半径），单位换算成您所希望使用的单位。

The equatorial radius of the Earth is approximately 3,963.2 miles or 6,378.1 kilometers.

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

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

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

## 距离系数 - Distance Multiplier¶

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

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
}
```