Paginate data with query cursors
With query cursors in 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.
Add a simple cursor to a query
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.
Web version 9
import { query, orderBy, startAt } from "firebase/firestore"; const q = query(citiesRef, orderBy("population"), startAt(1000000));
Web version 8
citiesRef.orderBy("population").startAt(1000000);
Swift
// Get all cities with population over one million, ordered by population. db.collection("cities") .order(by: "population") .start(at: [1000000])
Objective-C
// Get all cities with population over one million, ordered by population. [[[db collectionWithPath:@"cities"] queryOrderedByField:@"population"] queryStartingAtValues:@[ @1000000 ]];
Kotlin
Android
// Get all cities with a population >= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .startAt(1000000)
Java
Android
// Get all cities with a population >= 1,000,000, ordered by population, db.collection("cities") .orderBy("population") .startAt(1000000);
Dart
db.collection("cities").orderBy("population").startAt([1000000]);
Java
Python
Python
(Async)
C++
// Get all cities with a population >= 1,000,000, ordered by population, db->Collection("cities") .OrderBy("population") .StartAt({FieldValue::Integer(1000000)});
Node.js
Go
PHP
PHP
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.