Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 352
colin rickels Avatar asked Jun 01 '18 14:06

colin rickels


1 Answers

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.

like image 59
mickl Avatar answered Sep 17 '22 16:09

mickl