简体   繁体   English

mongodb:在集合内部的帖子中更新数组项

[英]mongodb: update array item inside of a post inside of a collection

I'm trying to figure out how to update an item inside an array which is a part of a post. 我试图弄清楚如何更新属于帖子一部分的数组中的项目。 I did find this which looks closest to what I'm looking for: MongoDB Update Array element 我确实找到了看起来最接近我想要的东西: MongoDB更新数组元素

But I dont have the specific post lined up first like this example seems to show. 但是我没有像这个例子所示的那样先排好特定的帖子。 But I need to find the specific post using _id first, then update an item in the array from the resulted post. 但是我需要先使用_id查找特定的帖子,然后从结果帖子中更新数组中的一项。

Can I do this with one query or do I need to find the item first to then update it like the example shows in the other answer? 我可以使用一个查询来执行此操作,还是需要先找到该项目然后再像另一个答案中的示例所示那样对其进行更新?

In the below json how could I update the first posts comments: first comment? 在下面的json中,我如何更新第一条评论:第一条评论?

EDIT: 编辑:

[{
    "_id": "52c06c86b78a091f26000001",
    "comments": [
        {
            "user_id": "52ad97bab142627664000001 ",
            "username": "phacer",
            "comment": "nice dress!!!",
            "created": 1390668491909,
            "_id": "_SvF-Ag4d6wFf9QR1KdAdFIpcCyI3cqm2F4rl3w7rdk="
        },
        {
            "user_id": "52ad97bab142627664000001 ",
            "username": "phacer",
            "comment": "nice dress!!!",
            "created": 1390668491909,
            "_id": "_SvF-Ag4d6wFf9QR1KdAdFIpcCyI3cqm2F4rl3w7rdk="
        }
    ],
    "desc": "Something somethingson",
    "hate": [],
    "imageurl": "some url",
    "love": [
        {
            "user_id": "52add4f4344e8ca867000001",
            "username": "asd",
            "created": 1390652212592
        }
    ],
    "tags": [
        {
            "y": 29.3,
            "brand": "Zara",
            "type": "Bow Tie",
            "x": 20,
            "color": "Black"
        }
    ],
    "user_id": "52add4f4344e8ca867000001"
},
{
    "_id": "52c06c86b78a091f26000001",
    "comments": [
        {
            "user_id": "52ad97bab142627664000001 ",
            "username": "phacer",
            "comment": "nice dress!!!",
            "created": 1390668491909,
            "_id": "_SvF-Ag4d6wFf9QR1KdAdFIpcCyI3cqm2F4rl3w7rdk="
        }
    ],
    "desc": "Something somethingson",
    "hate": [],
    "imageurl": "some url",
    "love": [
        {
            "user_id": "52add4f4344e8ca867000001",
            "username": "asd",
            "created": 1390652212592
        }
    ],
    "tags": [
        {
            "y": 29.3,
            "brand": "Zara",
            "type": "Bow Tie",
            "x": 20,
            "color": "Black"
        }
    ],
    "user_id": "52add4f4344e8ca867000001"
}]

您应该考虑使用$elemMatch文档 ),如下所示:

.update({'_id': '52c06c86b78a091f26000001', 'comments' : {$elemMatch: {'_id': '_SvF-Ag4d6wFf9QR1KdAdFIpcCyI3cqm2F4rl3w7rdk='}}}, {$set : {'comments.$.comment': 'Lame dress!'}})

您可以在Mongo中使用位置运算符来访问特定的数组元素,如下所示

db.collection.update( { _id: 52c06c86b78a091f26000001 }, { $set: { "comments.$1.comment" : "dress" } } ) 

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

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