firestore: get data

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.

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:

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
FIRCollectionReference * citiesRef = [ self . db collectionWithPath :@ "cities" ];
[[ 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():

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
FIRDocumentReference * docRef =
   
[[ 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" );
 
}
}];
 
Note:  If there is no document at the location referenced by 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).

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
FIRDocumentReference * docRef =
[[ 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" );
 
}
}];
 
Important:  Each custom class must have a public constructor that takes no arguments. In addition, the class must include a public getter for each property.

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:

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
[[[ self . db collectionWithPath :@ "cities" ] queryWhereField :@ "capital" isEqualTo :@( YES )]
    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:

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
[[ self . db collectionWithPath :@ "cities" ]
    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.

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
// Not available in the Objective-C client library.
 

Learn more about different types of queries.


猜你喜欢

转载自blog.csdn.net/leonqiu/article/details/79428096