0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Firestoreのcollection group内のdocument総数を無料で数える方法

0
Last updated at Posted at 2026-04-22

数えるのですら金を取るのか、Firestoreよ

count()はreadより1000倍安い。しかし

  • collection groupのindexがない
  • 1000倍安くてもものすごい数ある

こんなときは困る。

users/[user_id]/posts/[post_id]
となっているとき全ユーザーのpostsの総数を調べたいとき、count()するには posts に対してcollection group indexを作成しておかないといけないのである。indexはstorageコストにつながる。数え終わったらindex消せばいいという話ではあるのだが話を最後まで聞いてくれ。

答え

indexを消す。

たとえばpostsの総数が知りたいならpostsの何らかのindexを消した後に

% gcloud firestore operations list

これで

progressDocuments:
    completedWork: '100000000'
    estimatedWork: '100000000'

とかでてくる。この completedWork がたぶん総数だ。

消すindexがない?ないなら反対に適当な存在しないfieldでindexつくればいいんじゃないかな。知らない。

追記

「複合index」がある場合、そのfieldの組み合わせを含む、document数がわかる。
GCPのFirestore > インデックス。
たとえば isPublic, createdAt で複合indexを作った場合、それらを両方とも含むdocumentの数がわかる。それに加えてそのindexがfirestore storage容量をどれくらい使っているかもわかる。
単体indexも表示してほしいところですね (^o^)/~

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?