0

Suppose I have a collection in mongodb

{
  _id:124,
  array:[{
    _id:1
    name:'name1'
   },
   {
    _id:2
    name:'name2'
   }
  ]
}

How can i update name all the name field of Array 'array', with "Name" with single query? Thank you.

naruto
  • 327
  • 1
  • 11
  • Possible duplicate of [Update multiple elements in an array in mongodb](https://stackoverflow.com/questions/51206841/update-multiple-elements-in-an-array-in-mongodb) and [update multiple elements in array mongodb](https://stackoverflow.com/questions/51279886/update-multiple-elements-in-array-mongodb?rq=1) – Ashh Jul 26 '18 at 12:43
  • are you looking for renaming the array key `name` to `Name` ? – s7vr Jul 26 '18 at 12:51
  • no, the below answer works – naruto Jul 26 '18 at 12:53

1 Answers1

0

If you are using MongoDB 3.6 or more

db.collection.update({_id: 124}, {$set: {"array.$[].name" : "Name"}});

Checkout this Link for more

Suman Kundu
  • 1,702
  • 15
  • 22
  • [update api docs](http://mongoosejs.com/docs/api.html#query_Query-update) it doesn't look like they support it currently as arrayFilter is no mentioned in the available options, correct me if I'm wrong. – Suman Kundu Jul 26 '18 at 18:11
  • it seems, mongoose 5.x supports this. – naruto Jul 27 '18 at 04:21