in firestore/app/src/main/java/com/google/example/firestore/DocSnippets.java [1120:1194]
public void queryStartAtEndAt() {
// [START query_start_at_single]
// Get all cities with a population >= 1,000,000, ordered by population,
db.collection("cities")
.orderBy("population")
.startAt(1000000);
// [END query_start_at_single]
// [START query_end_at_single]
// Get all cities with a population <= 1,000,000, ordered by population,
db.collection("cities")
.orderBy("population")
.endAt(1000000);
// [END query_end_at_single]
// [START query_start_at_doc_snapshot]
// Get the data for "San Francisco"
db.collection("cities").document("SF")
.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
// Get all cities with a population bigger than San Francisco.
Query biggerThanSf = db.collection("cities")
.orderBy("population")
.startAt(documentSnapshot);
// ...
}
});
// [END query_start_at_doc_snapshot]
// [START query_pagination]
// Construct query for first 25 cities, ordered by population
Query first = db.collection("cities")
.orderBy("population")
.limit(25);
first.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
// ...
// Get the last visible document
DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
.get(documentSnapshots.size() -1);
// Construct a new query starting at this document,
// get the next 25 cities.
Query next = db.collection("cities")
.orderBy("population")
.startAfter(lastVisible)
.limit(25);
// Use the query for pagination
// ...
}
});
// [END query_pagination]
// [START multi_cursor]
// Will return all Springfields
db.collection("cities")
.orderBy("name")
.orderBy("state")
.startAt("Springfield");
// Will return "Springfield, Missouri" and "Springfield, Wisconsin"
db.collection("cities")
.orderBy("name")
.orderBy("state")
.startAt("Springfield", "Missouri");
// [END multi_cursor]
}