更新时出现Mongo错误:无法使用零件遍历元素 [英] Mongo error when updating: cannot use the part to traverse the element

查看:212
本文介绍了更新时出现Mongo错误:无法使用零件遍历元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用以下结构更新mongo集合

I am attempting to update my mongo collection with the following structure

{
    "_id" : "T6GqWsi9qSxnyGzgC",
    "spec_name" : "test",
    "version" : "test",
    "message_type" : "test",
    "message_trigger" : "test",
    "segments" : [
        {
            "segment_id" : "MSH",
            "sequences" : [
                {
                    "dataType" : "ST",
                    "optionality" : "R",
                    "name" : "Field Seperator"
                },
                {
                    "dataType" : "ST",
                    "optionality" : "R",
                    "name" : "Encoding Characters"
                },
                {
                    "dataType" : "HD",
                    "optionality" : "O",
                    "name" : "Sending Application"
                },
                {
                    "dataType" : "HD",
                    "optionality" : "O",
                    "name" : "Sending Facility"
                },
                {
                    "dataType" : "HD",
                    "optionality" : "O",
                    "name" : "Receiving Application"
                },
                {
                    "dataType" : "HD",
                    "optionality" : "O",
                    "name" : "Receiving Facility"
                },
                {
                    "dataType" : "TS",
                    "optionality" : "O",
                    "name" : "Data/Time Of Message"
                },
                {
                    "dataType" : "ST",
                    "optionality" : "O",
                    "name" : "Security"
                },
                {
                    "dataType" : "CM",
                    "optionality" : "R",
                    "name" : "Message Type"
                },
                {
                    "dataType" : "ST",
                    "optionality" : "R",
                    "name" : "Message Control ID"
                },
                {
                    "dataType" : "PT",
                    "optionality" : "R",
                    "name" : "Processing ID"
                },
                {
                    "dataType" : "NM",
                    "optionality" : "O",
                    "name" : "Sequence ID"
                },
                {
                    "dataType" : "ST",
                    "optionality" : "O",
                    "name" : "Continuation Pointer"
                },
                {
                    "dataType" : "ID",
                    "optionality" : "O",
                    "name" : "Accept Acknowledgment Type"
                },
                {
                    "dataType" : "ID",
                    "optionality" : "O",
                    "name" : "Application Acknowledgment Type"
                },
                {
                    "dataType" : "ID",
                    "optionality" : "O",
                    "name" : "Country Code"
                },
                {
                    "dataType" : "ID",
                    "optionality" : "O",
                    "name" : "Character Set"
                },
                {
                    "dataType" : "CE",
                    "optionality" : "O",
                    "name" : "Principal Language Of Message"
                }
            ]
        }
    ],
    "segment_groups" : [
        {
            "grouping_id" : 692335,
            "segments" : [
                {
                    "segment_id" : "test",
                    "group_segment_id" : 597268,
                    "sequences" : [ ]
                }
            ]
        }
    ]
}

我想附加{value_one: value,value_two: value}放入segment_groups中的序列数组。我的查询是这样的:

I wish to append { value_one: "value", value_two: "value" } into the sequences array within segment_groups. My query looks like this:

db.messages.update({"segment_groups.segments.group_segment_id": 597268},{$push:{ "segment_groups.segments.$.sequences":{"value_one":"value", "value_two":"value"}}})

尝试执行此操作时出现错误:

when attempting this i get the error:

"code" : 16837,
        "errmsg" : "cannot use the part (segment_groups of segment_groups.segments.0.sequences) to traverse the element ({segment_groups: [ { grouping_id: 692335, segments: [ { segment_id: \"test\", group_segment_id: 597268, sequences: [] } ] } ]})"
    enter code here

我已经在这个问题上待了一天,没有遇到任何关于如何解决或重组我的收藏的合理建议。我是mongo的新手,在构造时不了解最佳实践,因此使此方法成为更好解决方案所需的任何方法都很有价值。

Ive been at this issue for a day now and have no come across any sound advice on how to resolve or how to restructure my collection. I am new to mongo and am not aware of best practices when structuring so any method needed to make this a better solution is valuable.

推荐答案

您遇到的问题是您的模型具有两个嵌套数组级别( segments_groups segments ),而Mongo是

The problem you have is that your model has two levels of nested arrays (segments_groups and segments) and Mongo is not able to figure out which items in that arrays needs to be updated.

在MongoDB 3.6中,可以很容易地使用 arrayFilters /位置过滤运算符。在更新路径中,指定占位符代表每个数组的一个特定项,然后必须添加匹配条件作为 arrayFilters 来帮助MongoDB查找要更新的项。在您的情况下:

In MongoDB 3.6 it's easy to fix that using arrayFilters / positional filtered operator. In your update path you specify placeholders that represent one particular item for each array and then you have to add matching conditions as arrayFilters to help MongoDB finding which item you want to update. In your case:

db.messages.update({"_id" : "T6GqWsi9qSxnyGzgC"}, 
    { $push: { "segment_groups.$[sg].segments.$[s].sequences": {"value_one":"value", "value_two":"value"} } }, 
    { arrayFilters: [ { "sg.grouping_id": 692335 }, { "s.group_segment_id": 597268 } ] })

因此 $ [sg] 是一个特定的 segment_group $ [s] 代表一个细分。

So $[sg] is one particular segment_group and $[s] represents one segment.

这篇关于更新时出现Mongo错误:无法使用零件遍历元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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