MongoDB数据库采用bson的格式存储,存储的内容是文档型的。每一条记录就是一个文档,每个文档中的键不一定相同,键的个数也不一定相等,对这样嵌套的文档结构关系,进行分组统计就比较麻烦。比如有以下数据集
{"name": "test","num": {"text": 1,"face": 2}},
{"name": "test","num": {"image": 3,"face": 4}},
{"name": "test","num": {"text":5,"image": 6, "book": 12}},
{"name": "demo","num": {"text": 3,"face": 4}},
{"name": "demo","num": {"image": 7,"face": 8}},
{"name": "demo","num": {"text":5,"image": 5}}
希望获得各产品数量,查询结果为:
{"_id" : "test", "count" : { "image" : 9, "book" : 12, "face" : 6, "text" : 6} }
{"_id" : "demo", "count" : { "image" : 12, "text&
MongoDB 记录的字段不同时如何合并求和
![](https://img-home.csdnimg.cn/images/20240711042549.png)