数据结构:
以leagueId分组,统计文档下leagueFinishRecords数组字段下count值的和
db.league_exerciser_data.aggregate([
{ $unwind: "$leagueFinishRecords" },
{ $group: {
_id: "$leagueId",
total: { $sum: "$leagueFinishRecords.count" }
} }
]
)
db.集合名称.aggregate([
{ $unwind: "$数组字段名" },
{ $group: {
_id: "$更具那个字段分组,无需分组可以写null",
total: { $sum: "$数组字段名.数组下需要求和的字段" }
} }
]
以上sql语句运行结果: