LoginSignup
0
0

More than 3 years have passed since last update.

firebase firestoreで後からSubCollectionにfieldを追加したくなったときの対処。

Last updated at Posted at 2020-08-01

firebaseで後からfieldを追加するとき

例) サブコレクションとして保存しているcommentコレクションの全てのドキュメントに
status: 1
を追加したい。

var commentsSn = await firebase.firestore.collectionGroup("comments").get(); //snapshot取得
var commentsPath = commentsSn.docs.map(e => e.ref.path) //参照するパスを取得
 commentsPath.forEach(path => { //全てのパスに対して status:1 をセット
  firebase.firestore.doc(path).set({
    status:1
  },{
    merge: true
  })
})

optionで merge: true を付けておかないと他のフィールドが消えるのでご注意ください。

0
0
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
0
0