-
-
Notifications
You must be signed in to change notification settings - Fork 5
Firestore Paginate Data
The contents of this page are based on the original Firebase Documentation
With query cursors in Cloud Firestore, you can split data returned by a query into batches according to the parameters you define in your query.
Query cursors define the start and end points for a query, allowing you to:
- Return a subset of the data.
- Paginate query results.
However, to define a specific range for a query, you should use the where() method described in Simple Queries.
Use the startAt() or startAfter() methods to define the start point for a query. The startAt() method includes the start point, while the startAfter() method excludes it. For example, if you use startAt(A) in a query, it returns the entire alphabet. If you use startAfter(A) instead, it returns B-Z.
// Get all cities with population over one million, ordered by population.
db.collection("cities").order("population").startAt(1000);
Similarly, use the endAt() or endBefore() methods to define an end point for your query results.
// Get all cities with population less than one million, ordered by population.
db.collection("cities").order("population").endAt(1000);
To add more granularity to your cursor's start or end point, you can specify multiple conditions in the cursor clause. This is particularly useful if your data set includes fields where the first condition in a cursor clause would return multiple results. Use multiple conditions to further specify the start or end point and reduce ambiguity.
For example, in a data set containing all the cities named "Springfield" in the United States, there would be multiple start points for a query set to start at "Springfield":
Cities |
|
---|---|
Name |
State |
Springfield | Massachusetts |
Springfield | Springfield |
Springfield | Wisconsin |
To start at a specific Springfield, you could add the state as a secondary condition in your cursor clause.
// Will return all Springfields
db.collection("cities").order("name").order("state").startAt("Springfield");
// Will return "Springfield, Missouri" and "Springfield, Wisconsin"
db.collection("cities").order("name").order("state").startAt("Springfield", "Missouri");
Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.
Project setup
Analytics
Authentication
Dynamic Links
Google Sign In
Firestore
- Configuring the ANE
- Get Started
- Add and Manage Data
- Query Data
- Get Data
- Get Realtime Updates
- Perform Simple and Compound Queries
- Order and Limit Data
- Paginate Data
Messaging
One Signal
Performance
Remote Config
Storage
- Configuring the ANE
- Get Started
- Create a Reference
- Upload Files
- Download Files
- Use File Metadata
- Delete Files
Crashlytics
Vision
- Detect faces
- Scan barcodes
- Label images
- Recognize landmarks
- Natural Language
- Custom Models
External Links