$centerSphere
无法处理大距离,特别是如果必须绕过极点,请参阅此JIRA工单。
关于$geoWithin
,它不接受以英里为单位的距离,而是根据坐标系使用的单位测量圆的半径,因此经度和纬度如文档所述。这会导致一个非常大的边界框,包括阿尔布开克市
![bounding box](https://istack.dev59.com/qxRNl.webp)
您可以使用$near
,该操作符允许指定以米为单位的半径。例如,如果您将此用作测试数据:
db.places.insert( {
name: "Bee Cave, Texas",
location: { type: "Point", coordinates: [ -97.9524, 30.3061 ] }
} );
db.places.insert( {
name: "Austin, Texas",
location: { type: "Point", coordinates: [ -97.654724, 30.210768 ] }
} );
db.places.insert( {
name: "Albuquerque",
location: { type: "Point", coordinates: [ -106.621216, 35.113281 ] }
} );
db.places.createIndex( { location: "2dsphere" } )
你可以使用因子
1609.344
将英里转换为米,写出以下查询:
db.places.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: [-97.9524, 30.3061 ] },
$maxDistance: 20*1609.344
}
}
}
)
这个查询返回的结果既包括Bee Cave, TX也包括Austin, TX:
{
"_id":ObjectId("5a7190124f0cd7075d349bbc"),
"name":"Bee Cave, Texas",
"location":{
"type":"Point",
"coordinates":[
-97.9524,
30.3061
]
}
}{
"_id":ObjectId("5a7190124f0cd7075d349bbd"),
"name":"Austin, Texas",
"location":{
"type":"Point",
"coordinates":[
-97.654724,
30.210768
]
}
}
$near
。 - Alex