$centerSphere 无法处理很远的距离,特别是如果它必须绕到两极,请参阅此JIRA ticket。
关于$geoWithin,它不接受以英里为单位的距离,而是接受以坐标系使用的单位测量的圆的半径,因此纬度和经度按照documentation。这会产生一个非常大的边界框,其中确实包括阿尔伯克基
您使用$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
]
}
}