24
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

firestoreでaddせずにdocumentのunique idを使用する方法[備忘録]

Last updated at Posted at 2019-04-08

「Get a DocumentReference for the document within the collection at the specified path. If no path is specified, an automatically-generated unique ID will be used for the returned DocumentReference.」とのことで、下記のようにdocに引数渡さなければ良い

db.collection('hoge').doc().id

上記を用いれば下記のようにbatchで自動生成された共通のユニークidを使用して書き込める

const db = firebase.firestore()
const batch = db.batch()
const hogeId = db.collection('hoge').doc().id
batch.set(db.collection('hoge').doc(hogeId), values)
batch.set(
  db
    .collection('fuga')
    .doc('piyopiyo')
    .collection('hoge')
    .doc(hogeId),
  values)
24
14
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
24
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?