现在我有了一个这样的mongodb结构:
{
_id:
content:{
A:a
B:....
}
_id:
content:{
C:a
D:....
}
_id:
content:{
C:c
D:....
}
}我需要将content.A和content.C.相加,并对结果进行排序,得到前5名。预期结果如下:
a:2
c:1给我的Example.Better使用php mongo查询请。
发布于 2018-01-12 18:49:41
这应该可以让你开始:
collection.aggregate({
$group: {
_id: { $ifNull: [ "$content.A", "$content.C" ] }, // use "A" if it's available otherwise use "C"
count: { $sum: 1 } // count documents per group
}
}, {
$sort: { "count": -1 } // sort descending
}, {
$limit: 5 // get top 5 only
})https://stackoverflow.com/questions/48223680
复制相似问题