Find sum of fields inside array in MongoDB

后端 未结 1 1912
既然无缘
既然无缘 2021-01-22 13:46

I have a data as follows:

> db.PQRCorp.find().pretty()
{
    \"_id\" : 0,
    \"name\" : \"Ancy\",
    \"results\" : [
            {
                    \"eva         


        
相关标签:
1条回答
  • 2021-01-22 14:18

    You do not need to use $unwind and $group here... A simple $project query can $sum your entire score...

    db.PQRCorp.aggregate([
      { "$project": {
        "name": 1,
        "totalTermScore": {
          "$sum": "$results.score"
        }
      }}
    ])
    
    0 讨论(0)
提交回复
热议问题