Mongo error when updating: cannot use the part to traverse the element [duplicate]

依然范特西╮ 提交于 2020-08-07 06:14:11

问题


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" : [ ]
                }
            ]
        }
    ]
}

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

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.


回答1:


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.

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 } ] })

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



来源:https://stackoverflow.com/questions/50645179/mongo-error-when-updating-cannot-use-the-part-to-traverse-the-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!