Howto MongoDB更新/ $设置整个子文档

时间:2012-06-15 00:14:03

标签: mongodb database nosql

在我的MongoDB中,我有Document1,如下所示。我想进入“main.accounts”(以及后来的“main.entries”)文档,并在“内容”数组中更新($ set)整个子文档.Wrt子文档,到目前为止,我已经能够在“内容”子数组中添加和删除文档了。但在这种情况下,我想更新整个文档;不只是更新($ set)匹配文档中的一个属性。

下面,我尝试过只使用$ set,$ set使用$ elemMatch。我也试过findAndModify,但无济于事。当然,我是i)RTFM,环顾四周ii)互联网和iii)所以,找到一个解决方案。有没有人能够以这种方式更新整个文档?

由于 蒂姆

更新尝试1 - 这是唯一有效的更新,但将整个“内容”数组设置为哈希... {“fu”:“bar”}


    db.bookkeeping.update(
      { 
        owner : "twashing@gmail.com", 
        "content.content.tag" : "account", 
        "content.content.id" : "one" 
      }, 
      { $set : { "content.$.content" : { "fu" : "bar" } } } 
    )

更新试用2 - 这不会失败,但不会更新相关文档


    db.bookkeeping.update(
      { 
        owner : "fu", 
        thing : { $elemMatch : { "content.content.tag" : "account", "content.content.id" : "one"  } }
      }, 
      { $set : { "thing" : { "fu" : "bar" } } } 
    )

更新试用版3 - 这不会失败,但不会更新相关文档


    db.bookkeeping.update(
      { thing : { $elemMatch : { owner : "fu" ,  "content.content.tag" : "account", "content.content.id" : "one" } } }, 
      { $set : { "thing.content.content.$" :  { "fu" : "bar" } } }
    )

文档1


    {
            "_id" : "fid",
            "content" : [
                    {
                            "content" : [
                                    {
                                            "id" : "one",
                                            "tag" : "account"
                                    },
                                    {
                                            "id" : "two",
                                            "tag" : "account"
                                    },
                                    {
                                            "id" : "three",
                                            "tag" : "account"
                                    },
                                    {
                                            "id" : "four",
                                            "tag" : "account"
                                    },
                                    {
                                            "id" : "five",
                                            "tag" : "account"
                                    }

                            ],
                            "id" : "main.accounts",
                            "tag" : "accounts"
                    },
                    {
                            "tag" : "journals",
                            "id" : "main.journals",
                            "content" : [
                                    {
                                            "tag" : "journal",
                                            "id" : "generalledger",
                                            "name" : "generalledger",
                                            "type" : "",
                                            "balance" : "",
                                            "content" : [
                                                    {
                                                            "tag" : "entries",
                                                            "id" : "main.entries",
                                                            "content" : [ ]
                                                    }
                                            ]
                                    }
                            ]
                    }
            ],
            "owner" : "fu",
            "tag" : "fubar"
    }

2 个答案:

答案 0 :(得分:1)

您将无法使用位置运算符$来更新第二级数组。(数组内的数组)

This link可能会帮助您

答案 1 :(得分:-1)

我相信你不能在没有一些编程逻辑的情况下在mongoDB控制台中这样做,因为你在结构中使用多个数组并且你正在搜索一个子文档(内容[文档])(main.accounts和main.entries )在一个数组中,它将返回相同的根文档,但在该返回值中,您无法知道内容数组中的哪个位置是您的标准。因此,您无法在正确的位置注入新值。

如果可能,我建议重新构建文档以避免这么多嵌套数组(content-> content-> content->内容),特别是如果您打算对它们的子集进行一些更新。毕竟这是NoSQL,所以在你的设计中有一些冗余的数据是好的,只要你的生活更轻松,你的代码更干净。