LoginSignup
1
0

More than 1 year has passed since last update.

Cloud Firestoreでpythonでも集計クエリ(count)を使用する

Last updated at Posted at 2023-04-14

2022年10月にFirestoreでcount()が使用できるようになるアナウンスされました。
https://cloud.google.com/blog/ja/products/databases/aggregating-data-with-firestore

しかし、pythonでも使えることを本記事の執筆時点ではまだあまり情報が少ない状況です。
https://cloud.google.com/firestore/docs/query-data/aggregation-queries?hl=ja
スクリーンショット_2023-04-13_17_22_03.png

調べると、2023年2月のアップデートで利用できるようになっています。
https://firebase.google.com/support/release-notes/admin/python#version_610_-_02_february_2023
スクリーンショット_2023-04-13_17_24_32.png

実際に検証して確かにCOUNT可能でしたので、備忘録として残しておきます。

環境

まずはfirebase-adminをcountが使えるようになったv6.1.0にアップデートしましょう。

% python -V
Python 3.8.13

% pip list | grep firebase-admin        
firebase-admin                 6.1.0

% pip list | grep google-cloud-firestore
google-cloud-firestore         2.11.0

クエリしてみる

まずは何らかのコレクションリファレンスに対してqueryした状態です。

query = collection_ref.where("status", "==", True)

# <class 'google.cloud.firestore_v1.query.Query'>
print(type(query))

このqueryに対してcount()が使えるようになりました。
get()で取得出来ます。

count_get = query.count().get()
# [[<Aggregation alias=field_1, value=4, readtime=2023-04-13 08:36:05.086885+00:00>]]
print(count_get)

このままですとcountした数として扱えないので、以下のようにvalueだけ取り出します。

for count in count_get:
    # [<Aggregation alias=field_1, value=4, readtime=2023-04-13 08:36:05.086885+00:00>]
    print(count)
    # 4
    print(count[0].value)
    # <class 'int'>
    print(type(count[0].value))

まとめ

簡単にまとめすぎましたが、普段pythonでFirestoreを利用している方なら要点のみご理解いただける内容になっているかと思います。

料金も一致したインデックス エントリの数に対して課金され、最大1000件で1回分の読み取りとなるようなので、積極的に使っていきたい料金体系ですね。

トランザクションやAggregationには一切深堀りしていないので、また機会ある時に追記したいと思います。

【ソースコード】
https://github.com/googleapis/python-firestore

【公式リファレンス】
https://cloud.google.com/python/docs/reference/firestore/latest/query#countalias-strhttpspythonreadthedocsioenlatestlibrarystdtypeshtmlstr--nonehttpspythonreadthedocsioenlatestlibraryconstantshtmlnone--none

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