简体   繁体   English

(MongoDB)向文档添加一个新字段,其值为嵌套子文档数组的总和

[英](MongoDB) Adding a new field to a document with value as the sum of its nested sub-document array

I have following user collection document:我有以下用户收集文件:

user: {
  _id: ObjectId("...")
  name: "Kid",
  items: [
    {
       name: "pencil",
       type: "SCHOOL",
       amount: 1.00
    },
    {
       name: "computer",
       type: "PERSONAL",
       amount: 9999.99
    },
    {
       name: "notebook",
       type: "SCHOOL",
       amount: 9.00
    }
  ]
}

Now, I am trying to add a new field named schoolAmount to the top level document by summing amount in the subdocument that is of SCHOOL type.现在,我正在尝试通过对SCHOOL类型的子文档中的金额求和来将名为schoolAmount的新字段添加到顶级文档。 So in the above example document would have schoolAmount: 10.00 // (1 + 9) .所以在上面的示例文档中会有schoolAmount: 10.00 // (1 + 9) How could I make such a query?我怎么能做这样的查询? I am only aware of simple query like below using the shell.我只知道使用 shell 的简单查询如下所示。

db.user.updateMany({}, {$set: {"schoolAmount": <I need help here>}});

I assumed the document to look like what is inside of user.我假设文档看起来像用户内部的内容。 Then first doing aggregation and storing the output in cursor, then iterating over the cursor I can update the total.然后首先进行聚合并将 output 存储在 cursor 中,然后迭代 cursor 我可以更新总数。

var output = db.test.aggregate([{"$match":{name:"Kid"}},{"$unwind":"$items"},{"$match":{"items.type":"SCHOOL"}},{"$group":{"_id":"$name","total" :{$sum: "$items.amount"}}}])

while (output.hasNext()) {
   var doc = output.next();
   print(doc._id);
   print(doc.total);
   db.test.update({"name":doc._id},{$set:{"total":doc.total}})
}

Final doc最终文件

{
    "_id" : ObjectId("5f07e1616fddd269188c731c"),
    "name" : "Kid",
    "items" : [
        {
            "name" : "pencil",
            "type" : "SCHOOL",
            "amount" : 1
        },
        {
            "name" : "computer",
            "type" : "PERSONAL",
            "amount" : 9999.99
        },
        {
            "name" : "notebook",
            "type" : "SCHOOL",
            "amount" : 9
        }
    ],
    "total" : 10
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不重复的情况下将值添加到子文档中的数组(嵌套在主文档中)-MongoDB - Adding value to array in sub-document (nested within main doc) without duplication - MongoDB 在子文档数组中插入新字段 - Inset a new field in an array of sub-document 如何根据MongoDB中的条件投影子文档中字段的总和? - How to project a sum of a field in a sub-document based on a condition in MongoDB? 有没有办法匹配 mongodb 子文档数组中的 2 个特定字段值? - Is there a way to match 2 specific field values in a sub-document array in mongodb? Mongodb使用猫鼬检查数组子文档中的存在字段 - Mongodb checking exist field in an sub-document of an array using mongoose 如何从 MongoDB 的数组字段中删除特定的子文档? - How to delete a specific sub-document from an array field in MongoDB? MongoDB 有条件地按特定字段在数组中设置 $addToSet 子文档 - MongoDB conditionally $addToSet sub-document in array by specific field Mongodb 检查数组的子文档中是否存在字段 - Mongodb check If field exists in an sub-document of an array MongoDB - 更新数组的子文档以包含计算值 - MongoDB - Update the array's sub-document to include the calculated value MongoDB如何查询嵌套数组子文档不包含键值 - MongoDB how to query that nested array sub-document doesn't contain a key value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM