Firebase 9 - Firestore Method Cheat Sheet

6 min read
Mutations
// add doc await setDoc(doc(db, "pathname"), data); // upsert doc await setDoc(doc(db, "pathname"), data, { merge: true }); // update doc await updateDoc(doc(db, "pathname"), data); // add item to array await updateDoc(doc(db, "pathname"), { arrayname: arrayUnion("new value") }); // remove item from array await updateDoc(doc(db, "pathname"), { arrayname: arrayRemove("old value") }); // increment a number await updateDoc(doc(db, "pathname"), { counter: increment(1) }); // decrement a number await updateDoc(doc(db, "pathname"), { counter: increment(-1) }); // delete a doc await deleteDoc(doc(db, "pathname")); // create an id const id = doc(collection(db, 'id')).id;
Queries
// get a doc const docSnap = await getDoc(doc(db, "pathname")); const data = docSnap.exists() ? { ...docSnap.data(), id: docSnap.id } : null; // get multiple docs const querySnap = await getDocs(query(collection(db, "pathname"), ...filters)); for (const doc of querySnap.docs) { const data = doc.exists() ? { ...doc.data(), id: doc.id } : null; ... } // get collection Group multiple docs const querySnap = await getDocs(query(collectionGroup(db, "pathname"), ...filters); for (const doc of querySnap.docs) { const data = doc.exists() ? { ...doc.data(), id: doc.id } : null; ... } // filters where(field, comparison, value) orderBy(field, "desc|asc") limit(1) // cursors using field or docSnap startAt() startAFter() endAt() endBefore() // comparisons "<", "<=", "==", ">", ">=", "!=" // array comparisons "array-contains", "array-contains-any", "in", "not-in"
Realtime Queries
// get a doc const unsub = onSnapshot(doc(db, "pathname"), doc => { const data = doc.exists() ? { ...doc.data(), id: doc.id } : null; ... }); // get multiple docs (query is optional) const unsub = onSnapshot(query(collection(db, "pathname"), ...filters), querySnap => { for (const doc of querySnap.docs) { const data = doc.exists() ? { ...doc.data(), id: doc.id } : null; ... } ... });
Batches and Transactions
// execute a transaction const txData = await runTransaction(db, async (transaction) => { const docRef = doc(db, "pathname"); const docSnap = await transaction.get(docRef); const data = docSnap.exists() ? { ...doc.data(), id: doc.id } : null; const new_data = { ... }; // await not necessary due to tx transaction.update(docRef, new_data); // return new data return { ... }; }); // batch writes const batch = writeBatch(db); const docRef = doc(db, "pathname"); const docRef2 = doc(db, "pathname2"); const docRef3 = doc(db, "pathname3"); batch.set(docRef, { ... }); batch.update(docRef2, { ... }); batch.delete(docRef3); await batch.commit();
Note
All the above methods should be used within try / catch blocks for error checking. All of the unsub methods should be called within your framework's onDestroy methods to prevent memory leaks.
RXFire / Angular Fire
// get a doc const unsub = docData(doc(db, "pathname"), { idField: 'id' }) .subscribe(doc => { }); // get multiple docs const unsub = collectionData(query(collection(db, "pathname"), ...filters), { idField: 'id'}) .subscribe(docs => { }); // get a doc snap const unsub = docSnapshots(doc(db, "pathname"), { idField: 'id' }) .subscribe(doc => { }); // get a collection snap const unsub = collectionChanges(query(collection(db, "pathname"), ...filters), { idField: 'id'}) .subscribe(docs => { });
J




