0
0
GCPcloud~30 mins

Firestore document model in GCP - Mini Project: Build & Apply

Choose your learning style9 modes available
Firestore Document Model
📖 Scenario: You are building a simple app to store user profiles in Google Firestore. Each user has a unique ID, a name, and an email address. You want to create the Firestore document model to represent these users.
🎯 Goal: Create a Firestore document model by defining a dictionary that represents a user profile, add a configuration for the collection name, write the code to add the user document to Firestore, and complete the Firestore client initialization.
📋 What You'll Learn
Create a dictionary called user_profile with keys id, name, and email and exact values "user123", "Alice Smith", and "alice@example.com" respectively.
Create a variable called collection_name and set it to "users".
Write code to add the user_profile dictionary as a document to the Firestore collection named collection_name using the id as the document ID.
Initialize the Firestore client by importing the necessary module and creating a client instance called db.
💡 Why This Matters
🌍 Real World
Firestore is a popular NoSQL database used in many cloud applications to store and sync data in real time. Modeling documents correctly is essential for efficient data storage and retrieval.
💼 Career
Understanding Firestore document models and how to interact with Firestore using client libraries is a key skill for cloud developers and engineers working with Google Cloud Platform.
Progress0 / 4 steps
1
Create the user profile dictionary
Create a dictionary called user_profile with these exact entries: 'id': 'user123', 'name': 'Alice Smith', and 'email': 'alice@example.com'.
GCP
Need a hint?

Use curly braces to create a dictionary and separate keys and values with colons.

2
Set the Firestore collection name
Create a variable called collection_name and set it to the string 'users'.
GCP
Need a hint?

Assign the string 'users' to the variable collection_name.

3
Add the user document to Firestore
Write code to add the user_profile dictionary as a document to the Firestore collection named collection_name using the id field as the document ID. Use db.collection(collection_name).document(user_profile['id']).set(user_profile).
GCP
Need a hint?

Use the Firestore client db to access the collection and set the document.

4
Initialize the Firestore client
Import firestore from google.cloud and create a Firestore client instance called db using firestore.Client().
GCP
Need a hint?

Use the import statement and create the client before using it.