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).
The update with aggregation pipeline:
db.collection.updateOne(
{ },
[
{
$set: {
content: {
$function: {
body: function(content) {
for (let i = 0; i < content.length; i++) {
let tsr = content[i].teaser;
for (let j = 0; j < tsr.length; j++) {
tsr[j].uri = tsr[j].uri.replace('mydomain', 'newdomain');
}
content[i].teaser = tsr;
}
return content;
},
args: [ "$content" ],
lang: "js"
}
}
}
}
] )