使用时我没有得到正确的结果$geoNear
在聚合管道中。使用典型的 find() 查询的相同查询(使用$near
)实际上确实返回了正确的结果。
BUT,当删除相等条件时(在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" } );