1
1

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 3 years have passed since last update.

Firestore ドキュメントのIDって自分で設定できるんだ!!

Posted at

FirestoreのドキュメントIDって自分で設定できないの?

スクリーンショット 2021-01-07 23.00.16.jpg
FirestoreのドキュメントIDって、多くの人は自動生成されるやつを使ってるかと。

これをランダムな文字列じゃなくて、こっちで設定した文字列にする事はできないか?

やってみました。

docの引数を指定すればできた!

index.ts
let dt = new Date();
    let y = dt.getFullYear();
    let m = ('00' + (dt.getMonth() + 1)).slice(-2);
    const nowMonth = y + '' + m + '';

    const countsRef = db.collection(`users/${userId}/counts`).doc(nowMonth);

仮に上記のようなコードだった場合、ドキュメントの名前?(ID?)を「2021年1月」みたいな形にできます!
スクリーンショット 2021-01-07 23.05.37.jpg

本来はこういう使い方をするべきじゃないのかもれませんが、「毎月1つのドキュメントを生成する」という事をやりたかったので。。。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?