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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With