在这里,我有一系列像这样的重复项目
[ ‘GG’, ‘BB’, ‘DD’, “抄送”, ‘二’, ‘AA’, “抄送”, ‘GG’, ‘BB’, ‘DD’, “抄送”, ‘BB’, ‘DD’, ‘二’, ‘AA’,]而我愿意回来的就像……
使用 $开卷 和 $组 作为聚合pipiline的阶段: 的 查询: 强>
db.collection.aggregate([ { $unwind: "$items" }, { $group: { _id: "$items", count: { $sum: 1 } } } ])
的 结果: 强>
{ "_id": "ll", "count": 2 }, { "_id": "gg", "count": 2 }, { "_id": "bb", "count": 3 }, { "_id": "cc", "count": 3 }, { "_id": "aa", "count": 2 }, { "_id": "dd", "count": 3 }