So the uri is two array levels down (both content and teaser are arrays).
How can I replace all of these uri fields, i.e. iterate over both arrays? Is this possible without using map? And if not, how would the map version look like?
Wow, what an expression! Thank you so much for sharing this. That whole aggregation syntax is a mystery to me. I wish the docs would cover more of these “complex” cases than just the basics. So glad I found this forum.
Complex examples need some context on the problem to solve, so a lot of those will end up emerging via community discussion rather than being general recipes.
However, @Paul_Done, one of MongoDB’s most experienced Solution Architects, has recently released a free e-book covering Practical MongoDB Aggregations with great examples, tips, and cheat sheets:
This e-book is also linked from the Aggregation Reference documentation in the server manual, so hopefully that is a few steps closer to your wish .
Hi Stennie, thanks. I am totally aware of this. However, addressing and modifying objects inside arrays is something that should definitely be covered in the docs IMHO. I read though all the samples, but they only contain simple stuff like adding a value to all integers of an array. Also skimmed through Paul’s book yesterday, but couldn’t find examples for this either. But maybe I overlooked them.
Hello @waldgeist, here is another approach to update the nested array element field. This uses the $function aggregation operator. This operator allows define a custom JavaScript (JS) function (and use it within the aggregation). This requires MongoDB v4.4 or higher.
In this case use the JS function to iterate the outer and the inner arrays, and change the uri field value - but, using the JavaScript String#replace method (see String.replace at MDN).