0
0
GCPcloud~30 mins

Firestore collections and documents in GCP - Mini Project: Build & Apply

Choose your learning style9 modes available
Firestore Collections and Documents
📖 Scenario: You are building a simple app to store user profiles in Google Firestore. Each user has a unique ID and some basic information like name and email.
🎯 Goal: Create a Firestore collection called users and add documents with user data. Then configure a query to retrieve users with a specific email domain.
📋 What You'll Learn
Create a Firestore collection named users with three user documents
Each user document must have fields name and email with exact values
Add a configuration variable to filter users by email domain
Write a query to get all users whose email contains the configured domain
💡 Why This Matters
🌍 Real World
Managing user profiles in Firestore is common in apps that need to store and query user data efficiently.
💼 Career
Understanding Firestore collections, documents, and querying is essential for cloud developers working with Google Cloud Platform.
Progress0 / 4 steps
1
Create Firestore collection with user documents
Create a Firestore collection called users with three documents. Each document should have the fields: name and email with these exact values:

- Document ID: user1, name: Alice, email: alice@example.com
- Document ID: user2, name: Bob, email: bob@test.com
- Document ID: user3, name: Charlie, email: charlie@example.com
GCP
Need a hint?

Use client.collection('users') to get the collection reference. Use document('user1').set({...}) to add documents.

2
Add email domain filter configuration
Create a variable called email_domain and set it to the string '@example.com'. This will be used to filter users by their email domain.
GCP
Need a hint?

Just create a variable named email_domain and assign the string '@example.com'.

3
Write query to filter users by email domain
Write a query called query that gets all documents from the users collection where the email field contains the string stored in email_domain. Use Firestore's where method with the operator array_contains is not suitable here, so instead, retrieve all users and filter in Python using a list comprehension with email_domain in the email field.
GCP
Need a hint?

Firestore does not support 'contains' queries on strings. Retrieve all documents with stream() and filter in Python using a list comprehension.

4
Complete by printing filtered user names
Add a for loop to iterate over query and print the name field of each user document.
GCP
Need a hint?

Use a for loop over query and print the name field from each document's data.