LoginSignup
1
1

More than 5 years have passed since last update.

コンソールから Embedded Document の Array の各要素を更新する

Posted at

Array を更新するための Operator はいろいろとあるものの、$ を使用しても条件にマッチする first element しか更新できない。

条件にマッチする要素全てを同時に更新するためには、forEach を使って行う。

db.collection.find(
    { "arrays" : { $size: 2 }
).forEach( function(doc) {
    doc.arrays.forEach(function(element) {
      element.hoge = 'hige'
    })

    db.arrays.save(doc);
})

深い Embedded Document でも対応できるけど、なかなか面倒。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1