Day 2 of 5
⏱ ~60 minutes
Firebase in 5 Days — Day 2

Firestore: NoSQL Database

Read and write data in Firestore with real-time listeners and one-time queries.

Firestore Data Model

Firestore uses collections (like tables) and documents (like rows). Documents are JSON objects. Collections can contain subcollections. Think of it as a nested JSON tree.

CRUD operations
import { db } from './firebase';
import {
  collection, doc, getDocs, getDoc,
  addDoc, setDoc, updateDoc, deleteDoc,
  query, where, orderBy, limit,
  onSnapshot
} from 'firebase/firestore';

// Add a document (auto-generated ID)
const docRef = await addDoc(collection(db, 'posts'), {
  title: 'My Post',
  body: 'Content',
  userId: auth.currentUser.uid,
  createdAt: new Date()
});

// Set a document (known ID)
await setDoc(doc(db, 'users', userId), { name: 'Alice', role: 'user' });

// Update specific fields
await updateDoc(doc(db, 'posts', postId), { title: 'New Title' });

// Delete
await deleteDoc(doc(db, 'posts', postId));
Queries and Real-Time Listeners
// One-time query
const q = query(
  collection(db, 'posts'),
  where('userId', '==', currentUser.uid),
  orderBy('createdAt', 'desc'),
  limit(20)
);
const snapshot = await getDocs(q);
const posts = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));

// Real-time listener (auto-updates when data changes)
const unsubscribe = onSnapshot(q, (snapshot) => {
  const posts = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
  setPosts(posts);
});

// Cleanup: call unsubscribe() when component unmounts
📝 Day 2 Exercise
Build a Real-Time Blog
  1. C
  2. r
  3. e
  4. a
  5. t
  6. e
  7. a
  8. F
  9. i
  10. r
  11. e
  12. s
  13. t
  14. o
  15. r
  16. e
  17. c
  18. o
  19. l
  20. l
  21. e
  22. c
  23. t
  24. i
  25. o
  26. n
  27. f
  28. o
  29. r
  30. p
  31. o
  32. s
  33. t
  34. s
  35. .
  36. B
  37. u
  38. i
  39. l
  40. d
  41. a
  42. f
  43. o
  44. r
  45. m
  46. t
  47. o
  48. a
  49. d
  50. d
  51. p
  52. o
  53. s
  54. t
  55. s
  56. .
  57. D
  58. i
  59. s
  60. p
  61. l
  62. a
  63. y
  64. p
  65. o
  66. s
  67. t
  68. s
  69. i
  70. n
  71. r
  72. e
  73. a
  74. l
  75. -
  76. t
  77. i
  78. m
  79. e
  80. w
  81. i
  82. t
  83. h
  84. o
  85. n
  86. S
  87. n
  88. a
  89. p
  90. s
  91. h
  92. o
  93. t
  94. .
  95. A
  96. d
  97. d
  98. d
  99. e
  100. l
  101. e
  102. t
  103. e
  104. f
  105. u
  106. n
  107. c
  108. t
  109. i
  110. o
  111. n
  112. a
  113. l
  114. i
  115. t
  116. y
  117. .
  118. F
  119. i
  120. r
  121. e
  122. s
  123. t
  124. o
  125. r
  126. e
  127. S
  128. e
  129. c
  130. u
  131. r
  132. i
  133. t
  134. y
  135. R
  136. u
  137. l
  138. e
  139. s
  140. :
  141. o
  142. n
  143. l
  144. y
  145. a
  146. u
  147. t
  148. h
  149. e
  150. n
  151. t
  152. i
  153. c
  154. a
  155. t
  156. e
  157. d
  158. u
  159. s
  160. e
  161. r
  162. s
  163. c
  164. a
  165. n
  166. w
  167. r
  168. i
  169. t
  170. e
  171. .

Day 2 Summary

  • Firestore: collections → documents → subcollections. Documents are JSON.
  • addDoc (auto ID) vs setDoc (known ID). Both create/overwrite.
  • onSnapshot gives real-time updates. Returns an unsubscribe function — call it on cleanup.
  • Queries: where, orderBy, limit. Composite queries need an index (Firebase prompts you).
Finished this lesson?