使用聚合管道中的$geoNear
时,返回的结果不正确。但是,使用典型的find()查询(使用$near
)可以返回正确的结果。
但是,当移除等式条件(在schedule.key
上),两个查询都可以返回正确的数据。
使用聚合管道中的$geoNear
:
db.place.aggregate(
[
{
$geoNear: {
spherical: true,
near: { type: "Point", coordinates: [ 18.416145, -33.911973 ] },
distanceField: "dist"
}
},
{
$match: {
"schedule.key": { "$eq": "vo4lRN_Az0uwOkgBzOERyw" }
}
}
])
$near
查询语句:
db.place.find(
{
"point" : {
$near: {
type: "Point",
coordinates: [ 18.416145,-33.911973 ]
}
},
"schedule.key" : {
$eq : "vo4lRN_Az0uwOkgBzOERyw"
}
})
这个集合中的文档大致长这样:
{
"_id" : UUID("da6ccbb1-3c7a-45d7-bc36-a5e6007cd919"),
"schedule" : {
"_id" : UUID("587de5b7-a744-4b28-baa8-e6efb5f7f921"),
"key" : "vo4lRN_Az0uwOkgBzOERyw"
},
"point" : {
"type" : "Point",
"coordinates" : [
18.425102,
-33.922153
]
},
"name" : "Cape Town"
}
我已经在点字段上创建了适当的索引:
db.place.ensureIndex( { "point" : "2dsphere" } );