英文:
How to sum the values of fields?
问题
我有这样的结构体:
{
"actionName": "add_new_paint_layer",
"countUse": 1,
"sources": "smth"
},
{
"actionName": "clear",
"countUse": 1,
"sources": "smth"
},
{
"actionName": "clear",
"countUse": 5,
"sources": "smth"
}
如何对"actionName"为"clear"的"countUse"进行求和?
英文:
I have structs like this
{
"actionName": "add_new_paint_layer",
"countUse": 1,
"sources": "smth"
},
{
"actionName": "clear",
"countUse": 1,
"sources": "smth"
},
{
"actionName": "clear",
"countUse": 5,
"sources": "smth"
},
How to sum "countUse" for actionName "clear"?
答案1
得分: 1
db.collection.aggregate([
{$match:
{'actionName': 'clear'}},
{$group:
{_id: "$actionName",
total_count:{ $sum: "$countUse"}}}
])
英文:
db.collection.aggregate([
{$match:
{'actionName': 'clear'},
{$group:
{_id: "$actionName",
total_count:{ $sum: "$countUse"}}}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论