0
0
GCPcloud~10 mins

Firestore collections and documents in GCP - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Firestore collections and documents
Start
Create Collection
Add Document
Document Contains Fields
Add Subcollection?
YesCreate Subcollection
Add Document
Read/Update/Delete Document
End
This flow shows how you create a collection, add documents with fields, optionally add subcollections, and then read or modify documents.
Execution Sample
GCP
db.collection('users').doc('user1').set({name: 'Ana', age: 30})
This code creates a 'users' collection, adds a document with ID 'user1', and sets fields 'name' and 'age'.
Process Table
StepActionCollectionDocument IDFields AddedResult
1Create collection referenceusers--Reference to 'users' collection created
2Create document referenceusersuser1-Reference to document 'user1' created
3Set document fieldsusersuser1{name: 'Ana', age: 30}Document 'user1' created with fields
4Add subcollection reference?users/user1/orders--Subcollection 'orders' can be created under 'user1'
5Add document to subcollectionusers/user1/ordersorder1{item: 'book', qty: 1}Document 'order1' added to subcollection
6Read documentusersuser1-Fields returned: {name: 'Ana', age: 30}
7Update document fieldusersuser1{age: 31}Field 'age' updated to 31
8Delete documentusersuser1-Document 'user1' deleted
9Attempt to read deleted documentusersuser1-Document not found
10End---No more actions
💡 Execution stops after document deletion and failed read confirms removal.
Status Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 8After Step 9
users collectionemptycontains doc 'user1'contains doc 'user1' with subcollection 'orders'contains doc 'user1' with updated agedoc 'user1' deleteddoc 'user1' not found
user1 document fieldsnone{name: 'Ana', age: 30}{name: 'Ana', age: 30}{name: 'Ana', age: 31}deletednot found
orders subcollectionnonenonecontains doc 'order1'contains doc 'order1'contains doc 'order1'contains doc 'order1'
Key Moments - 3 Insights
Why does the document 'user1' still exist after adding a subcollection?
Adding a subcollection under 'user1' does not delete or replace the 'user1' document itself, as shown in steps 4 and 5 where 'user1' still has its fields.
What happens if you try to read a document after deleting it?
As seen in step 9, reading a deleted document returns 'Document not found' because the document no longer exists in the collection.
Can a collection exist without documents?
Yes, a collection reference can exist before any documents are added, as in step 1 where the 'users' collection is referenced but empty.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what fields does document 'user1' have after step 3?
A{name: 'Ana', age: 30}
B{item: 'book', qty: 1}
CNo fields, document is empty
DDocument deleted
💡 Hint
Check the 'Fields Added' column at step 3 in the execution table.
At which step does the 'age' field of 'user1' get updated?
AStep 3
BStep 7
CStep 5
DStep 9
💡 Hint
Look for the step where 'Update document field' action happens in the execution table.
If we delete the document 'user1' at step 8, what happens when we try to read it at step 9?
AReturns the original fields
BReturns updated fields
CReturns 'Document not found'
DReturns subcollection documents
💡 Hint
Check the 'Result' column at step 9 in the execution table.
Concept Snapshot
Firestore stores data in collections and documents.
Collections hold documents; documents hold fields.
Documents can have subcollections.
Create collection > add document with fields > optionally add subcollections.
Read, update, or delete documents anytime.
Deleting a document removes its fields but not necessarily its subcollections.
Full Transcript
Firestore organizes data in collections and documents. You start by creating a collection reference, then add documents identified by unique IDs. Each document holds fields, which are key-value pairs like name and age. Documents can also have subcollections, which are collections nested under a document. You can read, update, or delete documents. Deleting a document removes its fields and the document itself, but subcollections remain unless explicitly deleted. This flow helps manage structured data in Firestore efficiently.