使用MongoDB的,我怎么当元素位置未知更新子阵列的一个子文档? [英] Using MongoDB, how do I update a sub-document of a sub-array when element position is unknown?

查看:87
本文介绍了使用MongoDB的,我怎么当元素位置未知更新子阵列的一个子文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新子阵列的一个子文档,但在数组中,我不能对元素的位置计数始终是相同的,所以我怎么更新它。

I'm trying to update a sub-document of a sub-array, but I can't count on the element's position in the array to always be the same, so how do I update it.

例如,这里是我的文档,我只是想推另一个标记对象(键/ VAL)到标记阵列 real_pagenum :以 _id 的ObjectId的>的PDF 1(50634e26dc7c22c64d00000a)

For example, here's my document, I just want to push another tag object (key/val) onto the tags array of real_pagenum: 1 of the pdf with the _id of ObjectId("50634e26dc7c22c64d00000a")

{
    "_id" : ObjectId("503b83dfad79cc8d26000004"),
    "pdfs" : [
        {
            "_id" : ObjectId("50634e26dc7c22c64d00000a"),
            "pages" : [
                {
                    "real_pagenum" : 1,
                    "_id" : ObjectId("50634e74dc7c22c64d00002b"),
                    "tags" : [
                        {
                            "key" : "Item",
                            "val" : "foo"
                        },
                        {
                            "key" : "Item",
                            "val" : "bar"
                        }

                    ]

                },
                {
                    "real_pagenum" : 2,
                    "_id" : ObjectId("50634e74dc7c22c64d00002b")
                }
            ],
            "title" : "PDF3",
            "version" : 3
        }
    ],
}

要重申,我们的目标是第一个目标通过合适的PDF _id ,然后通过 real_pagenum 右页推成PDF格式的页面的标记阵列。

To reiterate, the goal is to first target the right pdf by _id, then the right page by real_pagenum and push into that pdf's page's tags array.

我试过:

db.projects.update({'pdfs.pdf_id':ObjectId("50634e25dc7c22c64d000007")},
    {$push:{'pages.$.tags':{'key':'foo','val':'bar'}}},false,false);

,但这并不到达我所需要的水平。我读过,我可以用实际元件位置位置运营商的组合,但再次,我不能保证或者知道元素的位置。

but that doesn't get to the level I need. I've read that I can use a combination of the positional operator with actual element position, but again, I can't guarantee or know the element's position.

推荐答案

这是目前不可能的。你只能解决您的文档树中的最高级别的阵列的位置运营商。如果你知道确切的元件位置的任何更深的阵列,才可以访问。

This is currently not possible. You can only address the highest level array in your document tree with the positional operator. Any deeper arrays can only be accessed if you know the exact element position.

这是目前一个悬而未决的问题: https://jira.mongodb.org/browse/SERVER -831

This is currently an open issue : https://jira.mongodb.org/browse/SERVER-831

这篇关于使用MongoDB的,我怎么当元素位置未知更新子阵列的一个子文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆