Order and limit data with Cloud FirestoreStay organized with collectionsSave and categorize content based on your preferences.
Cloud Firestoreprovides powerful query functionality for specifying which
documents you want to retrieve from a collection. These queries can also be used
with eitherget()oraddSnapshotListener(), as described inGet
Data.
Order and limit data
By default, a query retrieves all documents that satisfy the query in ascending
order by document ID. You can specify the sort order for your data usingorderBy(), and you can limit the number of documents retrieved usinglimit(). If you specify alimit(), the value must be greater than or equal
to zero.
For example, you could query for the first 3 cities alphabetically
with:
You can combinewhere()filters withorderBy()andlimit(). In the
following example, the queries define a population threshold, sort by population
in ascending order, and return only the first few results that exceed the
threshold:
However, if you have a filter with a range comparison (<,<=,>,>=),
your first ordering must be on the same field, see the list oforderBy()limitations below.
Limitations
Note the following restriction fororderBy()clauses:
A related effect applies to inequalities. A query with an inequality filter
on a field also implies ordering by that field. The following
query does not return documents without apopulationfield even
ifcountry = USAin that document . As a workaround, you can execute
separate queries for each ordering or you can assign a value for all fields
that you order by.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-11-14 UTC."],[],[]]