Creating Google Cloud Firestore Collections and Documents

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

You are tasked with developing a small web app to track a list of famous computer scientists. You want to store the details in a NoSQL document store so that you can interact with database records as JSON documents. If this app ever needs to scale, you need the database to be strongly consistent and globally available. To achieve this, you will create a Cloud Firestore database and connect to it with a Python Flask web app that runs in the Cloud Run serverless platform.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Activate Cloud Shell and the Required APIs
  1. From inside the GCP dashboard, load the Cloud Shell terminal. Your project should already be active in the terminal (highlighted in yellow), but if it isn’t, enter the following code using your project ID, which is located next to the Google Cloud Platform title at the top of the GCP dashboard:
    gcloud config set project <YOUR_PROJECT_ID>
  2. In the Cloud Shell terminal, using gcloud, activate the following APIs, which are required for this lab:
  3. Configure these settings for Cloud Run using gcloud config:
    • Set the region to us-east1
    • Set the platform to managed
Create a Firestore Collection and Documents

For this objective, we need to:

  1. Create a new Firestore database that is set to Native Mode and located in us-east1.
  2. Start a collection with the Collection ID of compscientists.
  3. Create the first document with the following information set on three lines:
    • Line one:
      • Field name: first
      • Field type: string
      • Field value: Ada
    • Line two:
      • Field name: last
      • Field type: string
      • Field value: Lovelace
    • Line three:
      • Field name: birthyear
      • Field type: number
      • Field value: 1815
  4. Create a second document for Alan Turing, born in 1912, using the same formatting from before for Field name and Field type, and update the Filed value with his information.
Deploy and Test the Flask Application

For this objective, you need your GCP project ID, which can be found next to the Google Cloud Platform title on the GCP dashboard:

  1. Clone the necessary git repo to get the Flask application code. This is our repo:
  2. Change to the flask-firestore directory.
  3. Build the demo app using gcloud, --tag,, and our project ID. Make sure to place it in the flask-firestore directory.
  4. Deploy flask-firestore to Cloud Run using --image,, and our project ID. Make sure it is placed in our flask-firestore directory without having to be authenticated.
  5. Use the returned URL for your newly deployed app.
  6. Browse the URL and view the demo page listing the documents in the collection.
  7. Add a new computer scientist (perhaps yourself!) to the collection using the web app’s form. Make sure you use proper names and a real year, as there is no exception handling in this web app.
  8. Check that the new document is listed both in the web app and in GCP.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?