MongoDB:更新子文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5646798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:03:19  来源:igfitidea点击:

MongoDB: Updating subdocument

mongodb

提问by kheya

I have this collection:

我有这个集合:

[{ "_id" : 7,
   "category" : "Festival",
   "comments" : [
        {
                "_id" : ObjectId("4da4e7d1590295d4eb81c0c7"),
                "usr" : "Mila",
                "txt" : "This is a comment",
                "date" : "4/12/11"
        }
    ]
}]

All I want is to push insert a new field inside comments like this:

我想要的只是在评论中推送插入一个新字段,如下所示:

[{ "_id" : 7,
   "category" : "Festival",
   "comments" : [
        {
                "_id" : ObjectId("4da4e7d1590295d4eb81c0c7"),
                "usr" : "Mila",
                "txt" : "This is a comment",
                "date" : "4/12/11",
                "type": "abc"  // find the parent doc with id=7 & insert this inside comments
        }
    ]
}]

How can I insert inside the comments subdocument?

如何在评论子文档中插入?

回答by Andrei Andrushkevich

You need to use the $ positional operator

您需要使用$ 位置运算符

For example:

例如:

update({ 
       _id: 7, 
       "comments._id": ObjectId("4da4e7d1590295d4eb81c0c7")
   },{
       $set: {"comments.$.type": abc}
   }, false, true
);

I didn't test it but i hope that it will be helpful for you.

我没有测试它,但我希望它会对你有所帮助。

If you want to change the structure of document you need to use

如果你想改变你需要使用的文档结构

db.collection.update( criteria, objNew, upsert, multi )

Arguments:

criteria - query which selects the record to update;
objNew - updated object or $ operators (e.g., $inc) which manipulate the object
upsert - if this should be an "upsert"; that is, if the record does not exist, nsert it
multi - if all documents matching criteria should be updated

db.collection.update(标准,objNew,upsert,multi)

参数:

criteria - query which selects the record to update;
objNew - updated object or $ operators (e.g., $inc) which manipulate the object
upsert - if this should be an "upsert"; that is, if the record does not exist, nsert it
multi - if all documents matching criteria should be updated

and insert new objNew with new structure. check this for more details

并插入具有新结构的新 objNew。检查这个以获取更多详细信息

回答by user2924017

The $ positional operator is only going to work as expected if the 'comments' field is NOT an array. The OP's json is malformed, but it looks like it could be an array.

如果 'comments' 字段不是数组,则 $ 位置运算符只会按预期工作。OP 的 json 格式错误,但看起来可能是一个数组。

The issue is that mongodb right now will only update the first element of an array which matches the query. Though there is an RFE open to add support for updating all matching array elements: https://jira.mongodb.org/browse/SERVER-1243

问题是 mongodb 现在只会更新与查询匹配的数组的第一个元素。尽管有一个 RFE 打开以添加对更新所有匹配数组元素的支持:https: //jira.mongodb.org/browse/SERVER-1243

To work around this issue with arrays you just have to do a regular find then update the elements in the array individually.

要解决数组的此问题,您只需执行常规查找,然后单独更新数组中的元素。