0
0
GCPcloud~10 mins

Firestore document model in GCP - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Firestore document model
Start: Create Collection
Add Document with ID
Add Fields (key-value pairs)
Document Stored in Collection
Query or Update Document
End
This flow shows how you create a collection, add a document with fields, and then store or query it in Firestore.
Execution Sample
GCP
collection = 'users'
doc_id = 'user123'
document = {'name': 'Alice', 'age': 30}
firestore.collection(collection).document(doc_id).set(document)
This code creates a 'users' collection, adds a document with ID 'user123' containing name and age fields.
Process Table
StepActionInputResultState Change
1Create collection reference'users'Collection 'users' readyCollection 'users' created
2Create document reference'user123'Document ref 'user123' readyDocument ref 'user123' created in 'users'
3Prepare document data{'name': 'Alice', 'age': 30}Data ready to storeData prepared with fields name and age
4Set document in FirestoreDocument ref + dataDocument stored in 'users/user123'Firestore now has document 'user123' with fields
5Query document'users/user123'Returns {'name': 'Alice', 'age': 30}Document data retrieved
6Update document fieldSet age=31Document updatedField 'age' changed to 31
7Query updated document'users/user123'Returns {'name': 'Alice', 'age': 31}Updated data retrieved
8End--Process complete
💡 Process ends after document is created, updated, and queried successfully.
Status Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
collectionundefined'users''users''users''users'
doc_idundefined'user123''user123''user123''user123'
documentundefined{'name': 'Alice', 'age': 30}{'name': 'Alice', 'age': 30}{'name': 'Alice', 'age': 31}{'name': 'Alice', 'age': 31}
firestore stateemptyemptyhas 'users/user123' with fieldshas 'users/user123' with updated agehas 'users/user123' with updated age
Key Moments - 3 Insights
Why do we need to specify a document ID when adding a document?
Specifying a document ID (see Step 2) lets you control the document's address in the collection. Without it, Firestore creates a random ID, which can make it harder to find or update later.
What happens if we update a field in the document?
Updating a field (Step 6) changes only that field's value in Firestore. The rest of the document stays the same, as shown by the variable_tracker where 'age' changes but 'name' remains.
Can a document exist without fields?
No, a document must have at least one field. In Step 3, we prepare fields before storing. Firestore requires data to store a document.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the document data after Step 4?
A{'name': 'Alice', 'age': 31}
B{}
C{'name': 'Alice', 'age': 30}
DNo data stored yet
💡 Hint
Check the 'Result' column at Step 4 in the execution_table.
At which step does the 'age' field get updated to 31?
AStep 6
BStep 4
CStep 3
DStep 7
💡 Hint
Look at the 'Action' and 'State Change' columns in the execution_table for field updates.
If we do not specify a document ID, what changes in the execution flow?
AStep 4 fails to store the document
BStep 2 is skipped and Firestore generates a random ID
CStep 3 changes the document data
DNo document is created
💡 Hint
Refer to Step 2 in the execution_table about document reference creation.
Concept Snapshot
Firestore Document Model:
- Collections hold documents.
- Documents have unique IDs and fields (key-value pairs).
- Create a collection, then add a document with fields.
- Update fields without replacing whole document.
- Query documents by collection and ID.
Full Transcript
This visual execution shows how Firestore stores data using collections and documents. First, a collection named 'users' is created. Then a document with ID 'user123' is prepared with fields 'name' and 'age'. This document is stored in Firestore under the 'users' collection. Later, the document is queried and the 'age' field is updated from 30 to 31. Each step updates the Firestore state and variables accordingly. Key points include the importance of document IDs, how fields are updated individually, and that documents must have fields to exist.