Firestore Perform Simple and Compound Queries - tuarua/Firebase-ANE GitHub Wiki
The contents of this page are based on the original Firebase Documentation
Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection. These queries can also be used with either getDocument() or addSnapshotListener(), as described in Get Data and Get Realtime Updates.
To get started, write some data about cities so we can look at different ways to read it back:
var citiesRef:CollectionReference = db.collection("cities");
citiesRef.document("SF").setData({
"name": "San Francisco",
"state": "CA",
"country": "USA",
"capital": false,
"population": 860000
});
citiesRef.document("LA").setData({
"name": "Los Angeles",
"state": "CA",
"country": "USA",
"capital": false,
"population": 3900000
});
citiesRef.document("DC").setData({
"name": "Washington D.C.",
"country": "USA",
"capital": true,
"population": 680000
});
citiesRef.document("TOK").setData({
"name": "Tokyo",
"country": "Japan",
"capital": true,
"population": 9000000
});
citiesRef.document("BJ").setData({
"name": "Beijing",
"country": "China",
"capital": true,
"population": 21500000
});
// Create a reference to the cities collection
var citiesRef:CollectionReference = db.collection("cities");
// Create a query against the collection.
var query:Query = citiesRef.where("state", "==", "CA");
The following query returns all the capital cities:
// The following query returns all the capital cities:
var capitalCities:Query = db.collection("cities").where("capital", "==", true);
The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be <, <=, ==, >, or >=.
Some example filters:
citiesRef.where("state", "==" , "CA");
citiesRef.where("population", "<", 100000);
citiesRef.where("name", ">=", "San Francisco");
You can also chain multiple where() methods to create more specific queries (logical AND). However, to combine the equality operator (==) with a range comparison (<, <=, >, or >=), make sure to create a custom index.
citiesRef.where("state", "==", "CO").where("name", "==", "Denver");
citiesRef.where("state", "==" ,"CA").where("population", "<", 1000000);