0
0
Firebasecloud~10 mins

Document-collection data model in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Document-collection data model
Start
Create Collection
Add Document
Document Contains Fields
Add Subcollection (optional)
Repeat for more documents or collections
Query or Update Documents
End
This flow shows how you start with a collection, add documents with fields, optionally add subcollections, and then query or update documents.
Execution Sample
Firebase
db.collection('users').doc('user1').set({name: 'Anna', age: 30})
This code creates a 'users' collection, adds a document 'user1' with fields name and age.
Process Table
StepActionCollectionDocument IDFields AddedResult
1Create collection if not existsusersCollection 'users' ready
2Create document 'user1'usersuser1Document 'user1' created
3Set fields in 'user1'usersuser1{name: 'Anna', age: 30}Fields saved in 'user1'
4Add subcollection 'orders' to 'user1'users/user1ordersSubcollection 'orders' created
5Add document 'order1' in 'orders'users/user1/ordersorder1{item: 'book', price: 10}Document 'order1' saved
6Query documents in 'users'usersReturns documents like 'user1'
7Update field 'age' in 'user1'usersuser1{age: 31}Field 'age' updated
8EndNo more actions
💡 All documents and collections created and updated as per steps.
Status Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
Collection 'users'emptycontains 'user1'contains 'user1' with subcollection 'orders'samesame
Document 'user1'none{name: 'Anna', age: 30}{name: 'Anna', age: 30, orders subcollection}{name: 'Anna', age: 31, orders subcollection}same
Subcollection 'orders'nonenonecontains 'order1'samesame
Document 'order1'nonenone{item: 'book', price: 10}samesame
Key Moments - 3 Insights
Why do we create a collection before adding documents?
Collections organize documents. Step 1 shows collection 'users' is ready before adding document 'user1' in step 2.
Can documents contain other collections?
Yes, documents can have subcollections. Step 4 shows adding 'orders' subcollection inside 'user1' document.
What happens when we update a field in a document?
The field value changes but the document stays. Step 7 updates 'age' from 30 to 31 in 'user1'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what fields does document 'user1' have after step 3?
A{name: 'Anna', age: 30}
B{item: 'book', price: 10}
C{age: 31}
DNo fields yet
💡 Hint
Check the 'Fields Added' column at step 3 in the execution table.
At which step is the subcollection 'orders' created under 'user1'?
AStep 2
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the action mentioning subcollection creation in the execution table.
If we change the update in step 7 to set age to 32, what changes in variable_tracker?
ADocument 'user1' age changes to 32 after step 7
BNo change in document 'user1'
CSubcollection 'orders' is removed
DCollection 'users' is deleted
💡 Hint
Check the 'Document user1' row in variable_tracker after step 7.
Concept Snapshot
Document-collection data model in Firebase:
- Collections hold documents.
- Documents hold fields (key-value pairs).
- Documents can have subcollections.
- Use .collection() and .doc() to navigate.
- Use .set() to add or update fields.
- Organize data hierarchically for easy queries.
Full Transcript
The document-collection data model in Firebase organizes data into collections and documents. You start by creating a collection, then add documents with fields inside it. Documents can also have subcollections, which are collections nested inside documents. You can add, update, and query documents easily. For example, creating a 'users' collection, adding a 'user1' document with name and age fields, then adding an 'orders' subcollection inside 'user1' with order documents. Updating a field changes its value but keeps the document intact. This model helps keep data structured and easy to access.