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.
Note: While the code samples cover multiple languages, the text explaining the samples refers to the Web method names.Example data
To get started, write some data about cities so we can look at different ways to read it back:
[[ citiesRef documentWithPath :@ "SF" ] setData :@{
@ "name" : @ "San Francisco" ,
@ "state" : @ "CA" ,
@ "country" : @ "USA" ,
@ "capital" : @( NO ),
@ "population" : @ 860000
}];
[[ citiesRef documentWithPath :@ "LA" ] setData :@{
@ "name" : @ "Los Angeles" ,
@ "state" : @ "CA" ,
@ "country" : @ "USA" ,
@ "capital" : @( NO ),
@ "population" : @ 3900000
}];
[[ citiesRef documentWithPath :@ "DC" ] setData :@{
@ "name" : @ "Washington D.C." ,
@ "country" : @ "USA" ,
@ "capital" : @( YES ),
@ "population" : @ 680000
}];
[[ citiesRef documentWithPath :@ "TOK" ] setData :@{
@ "name" : @ "Tokyo" ,
@ "country" : @ "Japan" ,
@ "capital" : @( YES ),
@ "population" : @ 9000000
}];
[[ citiesRef documentWithPath :@ "BJ" ] setData :@{
@ "name" : @ "Beijing" ,
@ "country" : @ "China" ,
@ "capital" : @( YES ),
@ "population" : @ 21500000
}];
Get a document
The following example shows how to retrieve the contents of a single document using get()
:
[[ self . db collectionWithPath :@ "cities" ] documentWithPath :@ "SF" ];
[ docRef getDocumentWithCompletion :^( FIRDocumentSnapshot * snapshot , NSError * error ) {
if ( snapshot != nil ) {
NSLog (@ "Document data: %@" , snapshot . data );
} else {
NSLog (@ "Document does not exist" );
}
}];
docRef
, the resulting document
will be null.
Custom objects
The previous example used getData()
to get the contents of the document as a map, but it's often more convenient to use a custom object type. In Add Data, you defined a City
class that you used to define each city. You can turn your document back into a City
object by calling .getData(City.class)
.
[[ self . db collectionWithPath :@ "cities" ] documentWithPath :@ "BJ" ];
[ docRef getDocumentWithCompletion :^( FIRDocumentSnapshot * snapshot , NSError * error ) {
FSTCity * city = [[ FSTCity alloc ] initWithDictionary : snapshot . data ];
if ( city != nil ) {
NSLog (@ "City: %@" , city );
} else {
NSLog (@ "Document does not exist" );
}
}];
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 where()
to query for all of the documents that meet a certain condition, then use get()
to retrieve the results:
getDocumentsWithCompletion :^( FIRQuerySnapshot * snapshot , NSError * error ) {
if ( error != nil ) {
NSLog (@ "Error getting documents: %@" , error );
} else {
for ( FIRDocumentSnapshot * document in snapshot . documents ) {
NSLog (@ "%@ => %@" , document . documentID , document . data );
}
}
}];
Get all documents in a collection
In addition, you can retrieve all documents in a collection by omitting the where()
filter entirely:
getDocumentsWithCompletion :^( FIRQuerySnapshot * snapshot , NSError * error ) {
if ( error != nil ) {
NSLog (@ "Error getting documents: %@" , error );
} else {
for ( FIRDocumentSnapshot * document in snapshot . documents ) {
NSLog (@ "%@ => %@" , document . documentID , document . data );
}
}
}];
List subcollections of a document
The getCollections()
method of the Cloud Firestore server client libraries lists all subcollections of a document reference.
Retrieving a list of collections is not possible with the mobile/web client libraries. You should only look up collection names as part of administrative tasks in trusted server environments. If you find that you need this capability in the mobile/web client libraries, consider restructuring your data so that subcollection names are predictable.
Learn more about different types of queries.