简体   繁体   English

如何在MongoDB中的嵌套子文档数组中添加新属性

[英]How to add new attribute to array of nested sub document in MongoDB

{
    "_id" : ObjectId("559f85351554febf038b5c70"),
    "company_name" : "Honey Crunch Popcorn Co",
    "tech" : [
        {
            "tech_name" : "Magento",
            "user" : "Rahul",
        },
        {
            "tech_name" : "Volusion",
            "user" : "Tina",
        }
    ]
}

I want to add new attribute "verified:true" to "tech" array for "tech_name" : "Volusion" only so it will result like this, 我只想将新属性“ verified:true”添加到“ tech_name”:“ Volusion”的“ tech”数组中,这样就可以得到结果,

{
    "_id" : ObjectId("559f85351554febf038b5c70"),
    "company_name" : "Honey Crunch Popcorn Co",
    "tech" : [
        {
            "tech_name" : "Magento",
              "user" : "Rahul"
        },
        {
            "tech_name" : "Volusion",
            "user" : "Tina",
            "verified" : "true"
        }
    ]
}

Please Help. 请帮忙。

To do this you use the $set operator in an update statement, whilst referencing the matched array element via the positional $ operator : 为此,您可以在update语句中使用$set运算符,同时通过位置$运算符引用匹配的数组元素:

db.collection.update(
    {
        "_id": ObjectId("559f85351554febf038b5c70"),
        "tech.tech_name": "Volution"
    },
    {
        "$set": {
            "tech.$.verified": true
        }
    }
)

So what that does is: 所以这样做是:

  1. The "query" or first argument portion of the .update() both finds a unique document ( not really required ) by field reference and secondly finds a field in the nested array my it's own matching "field/property". .update()的“查询”或第一个参数部分都通过字段引用找到唯一的文档(不是真正需要的),其次在嵌套数组中找到它自己的匹配“字段/属性”的字段。 This last part is important for the next stage. 最后一部分对于下一阶段很重要。

  2. The "update" portion of the .update() method here as the "second" argument contains the definition for "what gets modified". .update()方法的“ update”部分.update()此处为“ second”参数)包含“修改内容”的定义。 Using the $set operator here makes sure that only the referenced fields get modified in any way. 在此处使用$set运算符可确保仅以任何方式修改引用的字段。

    Then the "positional" $ operator here makes sure that only the "matched index" of that array element referenced from the query portion is the element that gets updated. 然后,这里的“位置” $运算符可确保只有从查询部分引用的数组元素的“匹配索引”才是要更新的元素。 This uses the "dot notation" form of 这使用了“点符号”形式

     $set: { "tech.$.verified": true } 

    That references the "correct" indexed field in the element and "creates" a new property where one does not already exist or otherwise "overwrites" an existing value. 这将引用元素中的“正确”索引字段,并“创建”一个不存在的新属性,否则将“覆盖”现有值。

Those are the basics of how this works. 这些是工作原理的基础。 The primary things to take away from here are: 从这里带走的主要事情是:

  1. Match an indexed value of an element in an array you wish to update and reference it via "dot notation". 匹配要更新的数组中元素的索引值,并通过“点符号”对其进行引用。

  2. Use the $set operation or other appropriate "update operator" for the field rather than providing a "raw" object structure in the "update" that would otherwise "overwrite" the existing object. 对字段使用$set操作或其他适当的“更新运算符” ,而不要在“更新”中提供否则将“覆盖”现有对象的“原始”对象结构。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM