0

I have document like below

{
_id="12345",
details:[
    {
        name:"Customer_Name",
        time:"request_time"
     },

    {
        name:"Customer_Name",
        time:"request_time"
    },
    {
        name:"Customer_Name",
        time:"request_time"
    }, ....
   ]
}

I want to update "name" field in all the objects of "details" array.

I can use it update each array object using

db.customer.updateMany({_id:"12345"},{$set:{"details.0.name":"My_Name"}});

Is there any way to update all of them at once

Supradeep
  • 161
  • 1
  • 11

1 Answers1

0

Try:

db.customer.find({ _id:"12345" })
    .forEach(function (doc) {
        doc.details.forEach(function (details) {
            details.name="My_Name";
        });
    db.customer.save(doc);
});
mrid
  • 5,782
  • 5
  • 28
  • 71