Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

K.7 Query Get Data

Hadi Tavakoli edited this page Dec 16, 2017 · 1 revision

Get Data with Cloud Firestore

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 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);

Get a document

The following example shows how to retrieve the contents of a single document using read():

var document:DocumentReference = Firestore.collection("cities").document("SF");
document.addEventListener(FirestoreEvents.DOCUMENT_GET_FAILURE, onReadFailure);
document.addEventListener(FirestoreEvents.DOCUMENT_GET_SUCCESS, onReadSuccess);
document.read();

function onReadFailure(e:FirestoreEvents):void
{
	trace("Read Failure: " + e.msg);
}

function onReadSuccess(e:FirestoreEvents):void
{
	if(e.documentSnapshot)
	{
		trace("DocumentSnapshot data: " + JSON.stringify(e.documentSnapshot.data));
	}
	else
	{
		trace("No such document");
	}
}

Note: If there is no document at the location referenced by docRef, the resulting document will be null.

Get multiple documents from a collection

You can also retrieve multiple documents with one request by querying documents in a collection. For example, you can use whereEqualTo() to query for all of the documents that meet a certain condition, then use read() to retrieve the results:

var query:Query = Firestore.collection("cities").whereEqualTo("capital", true);
query.addEventListener(FirestoreEvents.QUERY_GET_SUCCESS, onQueryReadSuccess);
query.addEventListener(FirestoreEvents.QUERY_GET_FAILURE, onQueryReadFailure);
query.read();

function onQueryReadSuccess(e:FirestoreEvents):void
{
	var q:Query = e.target as Query;
	query.removeEventListener(FirestoreEvents.QUERY_GET_SUCCESS, onQueryReadSuccess);
	query.removeEventListener(FirestoreEvents.QUERY_GET_FAILURE, onQueryReadFailure);
	
	for(var i:int=0; i < e.querySnapshot.documents.length; i++)
	{
		var snapshot:DocumentSnapshot = e.querySnapshot.documents[i];
		trace(JSON.stringify(snapshot.data));
	}
}

function onQueryReadFailure(e:FirestoreEvents):void
{
	var q:Query = e.target as Query;
	query.removeEventListener(FirestoreEvents.QUERY_GET_SUCCESS, onQueryReadSuccess);
	query.removeEventListener(FirestoreEvents.QUERY_GET_FAILURE, onQueryReadFailure);
	
	trace("Error getting documents: " + e.msg);
}

you can retrieve all documents in a collection by omitting the whereEqualTo() filter entirely

var collection:CollectionReference = Firestore.collection("cities");

Introduction to Firebase ANEs collection for Adobe Air apps


Get Started with Firebase Core in AIR

  1. Prerequisites
  2. Add Firebase to your app
  3. Add the Firebase SDK
  4. Init Firebase Core
  5. Available ANEs
  6. Managing Firebase iid

Get Started with Analytics

  1. Add Analytics ANE
  2. Init Analytics ANE
  3. Log Events
  4. Set User Properties

Get Started with Crashlytics

  1. Add Crashlytics ANE
  2. Test Your Implementation
  3. Customize Crash Reports
  4. Upload .dSYM for iOS apps

Get Started with DynamicLinks

  1. Add DynamicLinks ANE
  2. Init DynamicLinks ANE
  3. Create DynamicLinks
  4. Receive DynamicLinks
  5. View Analytics

Get Started with Authentication

  1. Add Authentication
  2. Init Authentication
  3. Manage Users
  4. Phone Number
  5. Custom Auth
  6. Anonymous Auth
  7. State in Email Actions
  8. Email Link Authentication

Get Started with FCM + OneSignal

  1. Add FCM ANE
  2. Init FCM ANE
  3. Send Your 1st Message
  4. Send Msg to Topics
  5. Understanding FCM Messages
  6. init OneSignal

Get Started with Firestore

  1. Add Firestore
  2. Init Firestore
  3. Add Data
  4. Transactions & Batches
  5. Delete Data
  6. Manage the Console
  7. Get Data
  8. Get Realtime Updates
  9. Simple and Compound
  10. Order and Limit Data
  11. Paginate Data
  12. Manage Indexes
  13. Secure Data
  14. Offline Data
  15. Where to Go From Here

Get Started with Realtime Database

  1. Add Realtime Database
  2. Init Realtime Database
  3. Structure Your Database
  4. Save Data
  5. Retrieve Data
  6. Enable Offline Capabilities

Get Started with Remote Config

  1. Parameters and Conditions
  2. Add Remote Config
  3. Init Remote Config

Get Started with Performance

  1. Add Performance ANE
  2. Init & Start Monitoring

Get Started with Storage

  1. Add Storage ANE
  2. Init Storage ANE
  3. Upload Files to Storage
  4. Download Files to Air
  5. Use File Metadata
  6. Delete Files

Get Started with Functions

  1. Write & Deploy Functions
  2. Add Functions ANE
  3. Init Functions
Clone this wiki locally