An aggregation query processes the data from multiple indexed entities to
return a single summary value. Firestore in Datastore mode supports the count()
aggregation query. Read this page to learn how to use the count()
aggregation
query.
count()
aggregation
Use the count()
aggregation to return the total number of indexed entities
that match a given query. This simplifies your application code and costs less than
counting by fetching each entity.
For example, this count()
aggregation returns the total number of entities
in a kind.
Java
GQL
AGGREGATE COUNT(*) AS total OVER ( SELECT * AS total FROM tasks )
GQL supports a simplified form of count()
queries:
SELECT COUNT(*) AS total FROM tasks
This example uses an optional alias of `AS total`.
The simplified form supports only FROM
and WHERE
clauses. See the GQL reference for more information.
The count()
aggregation takes into account any filters on the query and any
limit
clauses. For example, the following aggregation returns a count of
the number of entities that match the given filters.
Java
GQL
AGGREGATE COUNT(*) OVER ( SELECT * FROM tasks WHERE is_done = false AND tag = 'house')
GQL supports a simplified form of COUNT(*)
queries:
SELECT COUNT(*) AS total FROM tasks WHERE is_done = false AND tag = 'house'
This example uses an optional alias of `AS total`.
The simplified form supports only FROM
and WHERE
clauses. See the GQL reference for more information.
This example shows how to count up to a certain value. You can use this to, for example, stop counting at a certain number and inform users that they exceeded that number.
Java
GQL
AGGREGATE COUNT_UP_TO(1000) OVER ( SELECT * FROM tasks WHERE is_done = false)
GQL supports a simplified form of COUNT_UP_TO()
queries:
SELECT COUNT_UP_TO(1000) AS total FROM tasks WHERE is_done = false AND tag = 'house'
This example uses an optional alias of `AS total`.
The simplified form supports only FROM
and WHERE
clauses. See the GQL reference for more information.
Limitations
- The query you provide to the
count()
aggregation must meet the restrictions on queries. - Entities with array properties are not de-duplicated. Each array value that matches the query adds one to the count.
If a
count()
aggregation cannot resolve within 60 seconds, it returns aDEADLINE_EXCEEDED
error. Performance depends on your index configuration and on the size of the dataset.If the operation cannot be completed within the 60 second deadline, a possible workaround is to use cursors to merge multiple aggregations.
The
count()
aggregation reads from index entries and counts only indexed properties.Adding an
OrderBy
clause to the query limits the count to the entities where the sorting property exists.In GQL, the simplified form of
count()
does not supportORDER BY
,LIMIT
, orOFFSET
clauses.
Pricing
Pricing for count()
depends on the number of index entries
scanned during the operation. You are billed one entity read for up to
1,000 index entries matched. Subsequent index entries matched cost additional
read units. There is a minimum cost of one read unit for every query. For pricing
information, see Firestore in Datastore mode pricing.
What's next
- Learn about queries.
- Learn about best practices for Firestore in Datastore mode.