Get started with cloud firestore

Get Started with Cloud Firestore

This quickstart shows you how to set up Cloud Firestore, add data, then view the data you just added in the Firebase console.

Create a Cloud Firestore project

  1. Open the Firebase Console and create a new project.

  2. In the Database section, click Try Firestore Beta.

  3. Click Enable.

Cloud Firestore and App Engine: You can't use both Cloud Firestore and Cloud Datastore in the same project, which might affect apps using App Engine. Try using Cloud Firestore with a different project.

When you create a Cloud Firestore project, it also enables the API in the Cloud API Manager.

Set up your development environment

Add the required dependencies and client libraries to your app.

WEB
IOS
ANDROID
JAVA
PYTHON
NODE.JS
GO
  1. Follow the instructions to add Firebase to your iOS app.
  2. Add the Cloud Firestore pod to your Podfile
     
         
    pod 'Firebase/Core'
    pod
    'Firebase/Firestore'
  3. Save the file and run pod install.

Initialize Cloud Firestore

Initialize an instance of Cloud Firestore:

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
@import Firebase ;

// Use Firebase library to configure APIs
[ FIRApp configure ];

FIRFirestore * defaultFirestore = [ FIRFirestore firestore ];
 

Add data

Cloud Firestore stores data in Documents, which are stored in Collections. Cloud Firestore creates collections and documents implicitly the first time you add data to the document. You do not need to explicitly create collections or documents.

Create a new collection and a document using the following example code.

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
// Add a new document with a generated ID
__block
FIRDocumentReference * ref =
   
[[ self . db collectionWithPath :@ "users" ] addDocumentWithData :@{
     
@ "first" : @ "Ada" ,
     
@ "last" : @ "Lovelace" ,
     
@ "born" : @ 1815
   
} completion :^( NSError * _Nullable error ) {
     
if ( error != nil ) {
       
NSLog (@ "Error adding document: %@" , error );
     
} else {
       
NSLog (@ "Document added with ID: %@" , ref . documentID );
     
}
   
}];
 

Now add another document to the users collection. Notice that this document includes a key-value pair (middle name) that does not appear in the first document. Documents in a collection can contain different sets of information.

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
// Add a second document with a generated ID.
__block
FIRDocumentReference * ref =
   
[[ self . db collectionWithPath :@ "users" ] addDocumentWithData :@{
     
@ "first" : @ "Alan" ,
     
@ "middle" : @ "Mathison" ,
     
@ "last" : @ "Turing" ,
     
@ "born" : @ 1912
   
} completion :^( NSError * _Nullable error ) {
     
if ( error != nil ) {
       
NSLog (@ "Error adding document: %@" , error );
     
} else {
       
NSLog (@ "Document added with ID: %@" , ref . documentID );
     
}
   
}];
 

Read data

To quickly verify that you've added data to Cloud Firestore, use the data viewer in the Firebase console.

You can also use the "get" method to retrieve the entire collection.

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
   
[[ self . db collectionWithPath :@ "users" ]
    getDocumentsWithCompletion
:^( FIRQuerySnapshot * _Nullable snapshot ,
                                 
NSError * _Nullable error ) {
     
if ( error != nil ) {
       
NSLog (@ "Error getting documents: %@" , error );
     
} else {
       
for ( FIRDocumentSnapshot * document in snapshot . documents ) {
         
NSLog (@ "%@ => %@" , document . documentID , document . data );
       
}
     
}
   
}];
 

Secure your data

Watch a video tutorial

For detailed guidance on getting started with the Cloud Firestore mobile and web client libraries, watch one of the following video tutorials:

WEB
IOS
ANDROID

You can find more videos in the Firebase YouTube channel.

Next steps

Deepen your knowledge with the following topics:

  • Codelabs — Learn to use Cloud Firestore in a real app by following the codelab for AndroidiOS, or Web.
  • Data model — Learn more about how data is structured in Cloud Firestore, including hierarchical data and subcollections.
  • Add data — Learn more about creating and updating data in Cloud Firestore.
  • Get data — Learn more about how to retrieve data.
  • Perform simple and compound queries — Learn how to run simple and compound queries.
  • Order and limit queries Learn how to order and limit the data returned by your queries.

猜你喜欢

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