我有一个location集合,并且使用了下面的代码来显示分配的地理位置附近的位置:
db.location.find({ll:{$near:[-80.12996, 25.79315]}}).limit(5).pretty()
然而,现在我需要从这里扩展并找到排名前5的披萨店(字段类型:" pizza "),并按上面的geo位置排序(需要使用near才能按升序排序)。我不知道该怎么做。我试着使用aggregate,但对我不起作用,我可能把它的顺序搞错了。
发布于 2019-04-18 12:50:31
第1步:使用have location创建字段的索引。
db.location.createIndex( { <location field> : "2dsphere" } )第2步:尝试使用此查询替换稍后lng中坐标:
db.location.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [lat, lng]},
maxDistance:1000,
distanceField: "dist.calculated",
spherical: true
}
},
{
$match: {
type: "Pizza",
}
},
{
$sort: {
'dist.calculated': 1
}
},
{
$limit : 5
}
])https://stackoverflow.com/questions/55739055
复制相似问题