but in my case the field is deeply nested in an embedded document + array. How can I traverse the document and then update fields in an array of embedded documents. Structure is something like:
{
field_name: {
prop1: "prop",
nested_array_of_obj:[
{ field: 12 },
{ field: 66 },
]
}
}
and I want to transform those fields to arrays like:
{ field: [12]}
{ field: [66]}
I found that this almost works:
db.collection.updateMany(
{},
{ $set: { “field_name.nested_array_of_obj.$[].field”: [ “<value of the field???>”]}})
I tried $field_name.nested_array_of_obj.field" or with the [] and it puts that string directly as the value so I get
{ field: [ ‘$field_name.nested_array_of_obj.field’ ]}
not
{ field: [12] }
Thanks for the tip.
It is kind of the same, but really my question is about how to reference the value that is based on the field path expression into an array. I would like to know how this works, not just to solve the particular task at hand but also generally understand how the syntax works. Something like: { $set: { field: ["$field"] }
works but what happens when field is a more complex path expression into an array like in my example? { $set: { "some.nested.$[].array_field": [ <what goes here?> ] }
@John_Burkhardt1, is your issue resolve? If it is, please mark my post as the solution so that others know that it works. This will help keep this post useful and efficient. It is simply courtesy.