I’m sorry if this was not clear but my question about $addField.
I already know about the $set instruction.
$addField is supposed to handle this kind of update in a different manner that better fit my use case and the documentation states that I should be able to use it for my case.
You could then either get the aggregate pipeline to merge back in for the update or this is the documentation for using aggregation stages in the update operation:
I’ll have a play tomorrow with addFields instead see if I can get it working, unless someone else has an example to hand…
So "field.array.0.newField" is the syntax of a normal update query, the update with aggregation pipeline doesn’t have the privilege of using any of the syntax from normal update query syntax.
You can do something like this in an update with aggregation pipeline syntax,
$map to iterate a loop of field.array array
$arrayElemAt to get the first element’s value from field.array.itemId
$cond and $eq to check if the above first selected itemId value is equal to the current element’s itemId value then do the below merge operation otherwise return the same object $$this
$mergeObjects to merge the new property newField that you wanted to add and the current object $$this
@turivishal wrote “the update with aggregation pipeline doesn’t have the privilege of using any of the syntax from normal update query syntax”.
Do you have an official statement or documentation about specific dot notation differences between the aggregation and normal update pipeline ? From my point of view, this is either a bug or the documentation is not telling the whole story.
I appreciate your follow-up question. While I don’t find any explicit documentation that highlights the differences in dot notation between the aggregation pipeline and the normal update query syntax, it’s understandable that this can be a bit confusing.
The aggregation pipeline and the normal update query syntax are indeed separate mechanisms in MongoDB, each with its own set of rules and behaviors.
If you know what is the aggregation pipeline then it is easy to understand that an update with an aggregation pipeline supports an aggregation pipeline in an update query.
I have already provided the required resource links in my above post.