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 のコレクションの件数を取得する

0
Last updated at Posted at 2026-05-29

前提

  • gcloud が実行できる前提
  • APIコールで取得する

何度も使うんだったら python とかで書いたが方がいいとは思うけど。。

実行

コレクションの件数取得( API リクエスト )

PROJECT_ID=xxxxx
COLLECTION_ID=test_collection
curl -s \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  -X POST \
  "https://firestore.googleapis.com/v1/projects/${PROJECT_ID}/databases/(default)/documents:runAggregationQuery" \
  -d "{
    \"structuredAggregationQuery\": {
      \"structuredQuery\": {
        \"from\": [
          { \"collectionId\": \"${COLLECTION_ID}\" }
        ]
      },
      \"aggregations\": [
        {
          \"count\": {},
          \"alias\": \"total\"
        }
      ]
    }
  }" | jq -r '.[0].result.aggregateFields.total.integerValue'

ワンライナー版

PROJECT_ID=xxxx
COLLECTION_ID=test_collection
echo && echo $PROJECT_ID $COLLECTION_ID && date && curl -s -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" -X POST "https://firestore.googleapis.com/v1/projects/${PROJECT_ID}/databases/(default)/documents:runAggregationQuery" -d "{\"structuredAggregationQuery\":{\"structuredQuery\":{\"from\":[{\"collectionId\":\"${COLLECTION_ID}\"}]},\"aggregations\":[{\"count\":{},\"alias\":\"total\"}]}}" | jq -r '.[0].result.aggregateFields.total.integerValue' && echo
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?