如何对字段的值进行求和?

huangapple go评论101阅读模式
英文:

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"}}}
])

huangapple
  • 本文由 发表于 2017年8月4日 18:08:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/45504011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定