-
-
Notifications
You must be signed in to change notification settings - Fork 5
Firestore Get Data
The contents of this page are based on the original Firebase Documentation
There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries:
- Call a method to get the data.
- Set a listener to receive data-change events.
When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document changes.
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
});The following example shows how to retrieve the contents of a single document using getDocument():
var docRef:DocumentReference = db.collection("cities").document("SF");
docRef.getDocument(onDocSnapshot);
function onDocSnapshot(document:DocumentSnapshot, error:FirestoreError, realtime:Boolean):void {
if (document.exists){
}
}Note: If there is no document at the location referenced by docRef, the resulting document will be empty and calling exists on it will return false.
The previous example retrieved the contents of the document as a map, but in some languages it's often more convenient to use a custom object type. In AS3, you defined a City class that you used to define each city. You can turn your document back into a City object:
var docRef:DocumentReference = db.collection("cities").document("SF");
docRef.map(City);
docRef.getDocument(onDocSnapshot);
function onDocSnapshot(document:DocumentSnapshot, error:FirestoreError, realtime:Boolean):void {
if (document.exists){
var city:City = document.data as City;
}
}Important: Each custom class must have a public constructor that takes no arguments. In addition, the class must include properties, each declared as public.
You can also retrieve multiple documents with one request by querying documents in a collection. For example, you can use where() to query for all of the documents that meet a certain condition, then use getDocuments() to retrieve the results:
db.collection("cities").where("capital", "==", true)
.getDocuments(function (snapshot:QuerySnapshot, error:FirestoreError):void {
if (error) {
trace("Error getting documents: "+error);
return;
}
for each (var document:DocumentSnapshot in snapshot.documents) {
trace(document.id);
}
});In addition, you can retrieve all documents in a collection by omitting the where() filter entirely:
db.collection("cities").getDocuments(function (snapshot:QuerySnapshot, error:FirestoreError):void {
if (error) {
trace("Error getting documents: "+error);
return;
}
for each (var document:DocumentSnapshot in snapshot.documents) {
trace(document.id);
}
});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