'Update nested array object value by replacing substring of that object property

I have a following set of data in my mongodb collection called orders:

{
    "_id" : ObjectId("618e0e1b17687316dcdd6246"),
    "groupUID": "abc",
    "orderData" : {
        "charges" : {
            "total" : 18480.0,
            "subtotal" : 13980.0 
        },
        "items" : [ 
            {
                "name" : "Chocolate cookies",
                "imageURL": "domainURL2.com/cookies"
            },
            {
                "name" : "Chocolate muffins",
                "imageURL": "domainURL2.com/muffins"
            }
        ]
    }
}

Now I want to update the imageURL substring part of "domainURL2" to "domainURL1" field in every document of this collection. I have the following query so far:

db.orders.update( { "groupUID" : "abc" }, { "$set": { "orderData.items.$.imageURL":"myURL.com" } } )

I also have the query in javascript form but i want this to be in pure mongo query. So the query below will not do it for me unfortunately.

db.getCollection("orders").find({"groupUID" : "abc"}).forEach(function(aRow) {
    if (aRow.orderDetails !== undefined) {
    var updated = false;
        aRow.orderData.items.forEach(function(item) {

item.imageURL = item.imageURL.replace("eddress/", "noknok-app/");
        })
db.getCollection("orders").save(aRow);
    }
});

I want to update all record's imageURL field's substring part. I am unable to figure out the rest of the query. Can anyone please help me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source