MongoDB更新复杂文档

时间:2016-04-25 13:24:48

标签: mongodb

我搜索了一些类似的例子,但没有找到好的东西。



{
    "_id" : ObjectId("571dea148234534308a290e"),
    "user" : "myusername",
    "duration" : "",
    "test_details" : [ 
        {
            "test" : "New",
            "test_id" : "0",
            "comments" : "will be some text",
            "text" : ""
        }, 
        {
            "test" : "**to change value here**",
            "test_id" : "1",
            "comments" : "will be some text",
            "text" : "**to change value here**"
        }, 
        {
            "test" : "New",
            "test_id" : "2",
            "comments" : "will be some text",
            "text" : ""
        }, 
        {
            "test" : "New",
            "test_id" : "3",
            "comments" : "will be some text",
            "text" : ""
            "sub_test_details" : [ 
                {
                    "56eed334534566ac5668ca18" : [ 
                        {
                            "sub_test" : "New",
                            "sub_test_id" : "0",
                            "sub_comments" : "will be some text",
                            "sub_text" : ""
                        }, 
                        {
                            "sub_test" : "New",
                            "sub_test_id" : "1",
                            "sub_comments" : "will be some text",
                            "sub_text" : ""
                        }, 
                        {
                            "sub_test" : "New",
                            "sub_test_id" : "2",
                            "sub_comments" : "will be some text",
                            "sub_text" : ""
                        }
                    ]
                }, 
                {
                    "56eed3b1f37d66ac5668ca19" : [ 
                        {
                            "sub_test" : "New",
                            "sub_test_id" : "0",
                            "sub_comments" : "will be some text",
                            "sub_text" : ""
                        }, 
                        {
                            "sub_test" : "New",
                            "sub_test_id" : "1",
                            "sub_comments" : "will be some text",
                            "sub_text" : ""
                        }, 
                        {
                            "sub_test" : "**to change value here**",
                            "sub_test_id" : "2",
                            "sub_comments" : "will be some text",
                            "sub_text" : "**to change value here**"
                        }
                    ]
                } 
            ]
        }, 
        {
            "test" : "New",
            "test_id" : "4",
            "comments" : "will be some text",
            "text" : ""
        }
    ],
}




问题是:

  1. 什么是更新文字的查询="已成功完成"和测试="完成"其中_id" :ObjectId(" 571dea148234534308a290e")。test_id = 1.

  2. 什么是更新sub_text =" Sub Completed Successfully"和sub_test =" Sub Done"其中_id" :ObjectId(" 571dea148234534308a290e")。test_details.test_id = 3.sub_test_details.56eed3b1f37d66ac5668ca19.sub_step_id = 2

  3. 在我的情况下,我将获得 id test_id exam_num(56eed3b1f37d66ac5668ca19) sub_test_id 将是参数找到相关的更新地点。 数组元素的大小并不总是恒定的。因此我使用id来准确指定我要查找的元素。

    由于

2 个答案:

答案 0 :(得分:2)

MongoDB允许在更新嵌套文档时使用索引,因此第一个问题的查询应该很可能是这样的:

db.test.update({"_id": ObjectId("571dea148234534308a290e"), "test_details.test_id": "1"}, {"$set": {"test_details.$.test": "Done", "test_details.$.text": "Completed Succesfully"}})

不幸的是,这仅适用于第一级嵌入,因此您不能使用多个位置运算符$。在这种情况下,有两种方法可以更改嵌入数据:首先,您应该知道要更新的文档的索引,第二种方法需要更改一些数据结构。而不是使用列表来保存test_details中的嵌套文档,您应该以这种方式使用字典:

{
    "test" : "New",
    "test_id" : "3",
    "text" : ""
    "sub_test_details" : {
        "56eed334534566ac5668ca18" : {
            "0": {
                "sub_test" : "New",
                "sub_text" : ""
            }, 
            "1": {
                "sub_test" : "New",
                "sub_text" : ""
            }, 
            "2": {
                "sub_test" : "New",
                "sub_text" : ""
            }
        },
        "56eed3b1f37d66ac5668ca19" : {
            "0": {
                "sub_test" : "New",
                "sub_text" : ""
            }, 
            "1": {
                "sub_test" : "New",
                "sub_text" : ""
            }, 
            "2": {
                "sub_test" : "**to change value here**",
                "sub_text" : "**to change value here**"
            }
        }
    } 
}

,查询将如下:

db.test.update({"_id": ObjectId("571e32c0907445669f11037e"), "test_details.test_id": "3"}, {"$set": {"test_details.$.sub_test_details.56eed334534566ac5668ca18.0.sub_test": "Done", "test_details.$.sub_test_details.56eed334534566ac5668ca18.0.sub_text": "Completed Succesfully"}})

另见this问题,讨论类似问题

答案 1 :(得分:0)

更新方法应该为你做,你应该使用$ addToSet,以确保没有重复进入给定id的数组,并且upsert:true来更新具有相同id的现有文档。以下应该有效

db.tests.update(
    {_id" : ObjectId("571dea148234534308a290e"}, 
    {$addToSet: 
        { test_details: 
            {test_id: "0", 
             test: "Done", 
             text: "Completed Successfully" 
   }}}, 
   {upsert: true}
);