🐶
Firestre Collection内document数の取得方法【Python】
はじめに
Pythonライブラリにおける日本語で説明されたものが見当たらなかったので、その備忘録。
内容
以下、サンプルコードを示します。
# from google.oauth2.service_account import Credentials
from google.cloud import firestore
## ローカル環境下ではapi key が必要です。クラウドで動作させる際はサービスアカウントをセットし、適切にロールを設定すれば必要ありません。
# api_key = "~~.json"
# credential = Credentials.from_service_account_file(api_key)
collection = firestore.Client(
# credentials=credential ## 理由は上と同じ
).collection("p_hazard").get()
length = len(list(collection))
print(length)
参考
- Firestore - Count number of documents in a collection:https://github.com/googleapis/google-cloud-python/issues/5731
Discussion