【问题标题】:MongoDB update complex documentMongoDB更新复杂文档
【发布时间】:2016-08-18 22:09:14
【问题描述】:

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

{
    "_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. 什么是更新 text="Completed Successfully" 和 test="Done" where _id" 的查询:ObjectId("571dea148234534308a290e").test_id=1。

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

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

谢谢

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    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 讨论类似问题的问题

    【讨论】:

      【解决方案2】:

      update 方法应该为您执行此操作,您应该使用 $addToSet,以确保没有重复项进入给定 id 的数组,并使用 upsert:true 更新具有相同 id 的现有文档。以下应该可以工作

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

      【讨论】:

        猜你喜欢
        • 2016-06-28
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 1970-01-01
        • 2017-01-30
        • 2013-03-03
        • 1970-01-01
        相关资源
        最近更新 更多