-
Notifications
You must be signed in to change notification settings - Fork 16
K.8 Simple and Compound Queries
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 read() 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 cities:CollectionReference = Firestore.collection("cities");
var data1:Object = {};
data1.name = "San Francisco";
data1.state = "CA";
data1.country = "USA";
data1.capital = false;
data1.population = 860000;
cities.document("SF").write(data1);
var data2:Object = {};
data2.name = "Los Angeles";
data2.state = "CA";
data2.country = "USA";
data2.capital = false;
data2.population = 3900000;
cities.document("LA").write(data2);
var data3:Object = {};
data3.name = "Washington D.C.";
data3.state = null;
data3.country = "USA";
data3.capital = true;
data3.population = 680000;
cities.document("DC").write(data3);
var data4:Object = {};
data4.name = "Tokyo";
data4.state = null;
data4.country = "Japan";
data4.capital = true;
data4.population = 9000000;
cities.document("TOK").write(data4);
var data5:Object = {};
data5.name = "Beijing";
data5.state = null;
data5.country = "China";
data5.capital = true;
data5.population = 21500000;
cities.document("BJ").write(data5);
The following query returns all cities with state CA:
// Create a reference to the cities collection
var citiesRef:CollectionReference = Firestore.collection("cities");
// Create a query against the collection.
var query:Query = citiesRef.whereEqualTo("state", "CA");
The following query returns all the capital cities:
var capitalCities:Query = Firestore.collection("cities").whereEqualTo("capital", true);
Some example filters:
citiesRef.whereEqualTo("state", "CA");
citiesRef.whereLessThan("population", 100000);
citiesRef.whereGreaterThanOrEqualTo("name", "San Francisco");
You can also chain multiple where() methods to create more specific queries (logical AND).
citiesRef.whereEqualTo("state", "CO").whereEqualTo("name", "Denver");
citiesRef.whereEqualTo("state", "CA").whereLessThan("population", 1000000);
To learn more about valid and invalid queries, read here.
Enjoy building Air apps – With ♥ from MyFlashLabs Team
Introduction to Firebase ANEs collection for Adobe Air apps
Get Started with Firebase Core in AIR
- Prerequisites
- Add Firebase to your app
- Add the Firebase SDK
- Init Firebase Core
- Available ANEs
- Managing Firebase iid
Get Started with Authentication
- Add Authentication
- Init Authentication
- Manage Users
- Phone Number
- Custom Auth
- Anonymous Auth
- State in Email Actions
- Email Link Authentication
Get Started with FCM + OneSignal
- Add FCM ANE
- Init FCM ANE
- Send Your 1st Message
- Send Msg to Topics
- Understanding FCM Messages
- init OneSignal
- Add Firestore
- Init Firestore
- Add Data
- Transactions & Batches
- Delete Data
- Manage the Console
- Get Data
- Get Realtime Updates
- Simple and Compound
- Order and Limit Data
- Paginate Data
- Manage Indexes
- Secure Data
- Offline Data
- Where to Go From Here
Get Started with Realtime Database
- Add Realtime Database
- Init Realtime Database
- Structure Your Database
- Save Data
- Retrieve Data
- Enable Offline Capabilities
Get Started with Remote Config
- Add Storage ANE
- Init Storage ANE
- Upload Files to Storage
- Download Files to Air
- Use File Metadata
- Delete Files