Skip to content

Firestore Get Started

Eoin Landy edited this page Jul 2, 2018 · 2 revisions

The contents of this page are based on the original Firebase Documentation

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 the Get Started button for Cloud Firestore.
  3. Select a starting mode for your Cloud Firestore Security Rules:

Test mode

Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing, make sure to see the Secure your data section.
To get started with the Web, IOS, or Android SDK, select test mode.

Locked mode

Denies all reads and writes from mobile and web clients. Your authenticated application servers (C#, Go, Java, Node.js, PHP, or Python) can still access your database.
To get started with the C#, Go, Java, Node.js, PHP, or Python server client library, select locked mode.
  1. 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.

Initialize Cloud Firestore

Initialize an instance of Cloud Firestore:

import com.tuarua.firebase.FirestoreANE;

private var db:FirestoreANE;

db = FirestoreANE.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.

// Add a new document with a generated ID
var ref:DocumentReference;
ref = db.collection("users").addDocument({
    "first": "Ada",
    "last": "Lovelace",
    "born": 1815
}, function (path:String, error:FirestoreError):void {
    if (error) {
        trace("Error adding document: " + error);
    } else {
        trace("Document added with ID: " + ref.id);
    }
});

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.

// Add a second document with a generated ID.
var ref:DocumentReference;
ref = db.collection("users").addDocument({
    "first": "Alan",
    "middle": "Mathison",
    "last": "Turing",
    "born": 1912
}, function (path:String, error:FirestoreError):void {
    if (error) {
        trace("Error adding document: " + error);
    } else {
        trace("Document added with ID: " + ref.id);
    }
});

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 getDocuments method to retrieve the entire collection.

db.collection("users").getDocuments(function (snapshot:QuerySnapshot, error:FirestoreError):void {
    if (error) {
        trace("Error getting documents: " + error);
    }else {
        for each (var document:DocumentSnapshot in snapshot.documents) {
            trace(document.id)
        }
    }
});

Secure your data

Use Firebase Authentication and Cloud Firestore Security Rules to secure your data in Cloud Firestore.

Here are some basic rule sets you can use to get started. You can modify your security rules in the Rules tab of the console.

Auth required
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}
Locked mode
// Deny read/write access to all users under any conditions
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}
Test mode
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
Clone this wiki locally