这是查询,我尝试进行汇总,我的要求是获取总记录数以及具有15个限制的分页。我得到了结果,但似乎有重复项。请指出我在想什么。
db.collections.aggregate([
{
"$match": {
"application_id": "some_app_id"
}
},
{
"$lookup": {
"localField": "_id",
"from": "collection1",
"foreignField": "some_id",
"as": "merged"
}
},
{
"$unwind": {"path": "$merged"}
},
{
"$group": {
"_id": null,
"count": {"$sum": 1},
"posts": {
"$push":"$$ROOT"
}
}
},
{
"$unwind": "$posts"
},
{
"$group": {
"_id": "$posts._id",
"data":{"$addToSet":"$posts"},
"count": {"$first" : "$count"},
"posts": {
"$push":"$$ROOT"
}
}
},
{
"$project":{
"_id": 1,
"count": 1,
"data": 1
}
}
])
实际输出
“ count”:89 //这里的计数实际上是33,但我以某种方式得到89
预期输出
“计数”:33,无重复