我有以下文件。
{
"_id": {
"$oid": "526fdc1fd6b0a8182300009c"
},
"body": "test abc",
"emb" : [{"body":"text","em":"abc.com","auth":"XYZ"},
{"body":"text","em":"abc.com","auth":"ABC"}
]
}
{
"_id": {
"$oid": "526fdc1fd6b0a8182300009d"
},
"body": "test abc",
"emb" : [{"body":"text","em":"abc.com","auth":"PQR"},
{"body":"text","em":"abc.com","auth":"ABC"}
]
}
如果我想计算内部文档数组中每个“ auth”的出现次数,该怎么办?我期望的结果是
"ABC":2
"PQR":1
"XYZ":1
$unwind
具有的emb
数组{$unwind: "$emb"}
emb.auth
时用{$group: { _id: "$emb.auth", count: { $sum:1 } } }
尽管语法略有不同,但这会为您提供所需的信息:
{ _id:"ABC", count:2 },
{ _id:"PQR", count:1 },
{ _id:"XYZ", count:1 }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句