我正在研究mongodb。
如何在排序$geoNear
之后对子文档进行排序?
我想将此查询转换为弹性搜索,但是我无法分页显示每页20条评论。
db.getCollection('ratings').aggregate([
{
// find review near user location
$geoNear: {
near: { type: "Point", coordinates: [ 108.220629394054 , 16.0727167550719 ] },
distanceField: "dist.calculated",
maxDistance: 1000000,
query: { "score" : {$gte: 2} },
includeLocs: "dist.location",
num: 100000,
spherical: true
},
},
{
// group review by business
$group: {
_id: "$business",
reviewIDs: { $push: {reviewID: "$_id", score: "$score", feedback: "$feedback"}}
}
},
{
// I want sort review by score before get 5 top hits
$sort: {"$reviewIDs.score": -1 }
},
{
// top 5 review have best score
$project: {
reviewIDs: { $slice: [ "$reviewIDs", 5 ] }
}
},
{
$unwind: "$reviewIDs"
},
{
$limit: 100
},
])