How do you write data to Firestore?

Answer

Firestore provides three write methods: (1) setDoc() — creates or completely overwrites a document: await setDoc(doc(db, "users", "alice"), { name: "Alice", email: "alice@example.com" }). Specify { merge: true } to merge instead of overwrite; (2) addDoc() — creates a document with auto-generated ID: const ref = await addDoc(collection(db, "posts"), { title: "Hello", author: "alice" }); console.log(ref.id); (3) updateDoc() — updates specific fields without overwriting the entire document: await updateDoc(doc(db, "users", "alice"), { lastSeen: serverTimestamp() }). If the document doesn't exist, updateDoc fails (use setDoc with merge for upsert behavior). Use serverTimestamp() for timestamps to avoid client-side clock skew. Field paths with dots ("address.city") update nested map fields without overwriting sibling fields.