Firestore Web

Firestore Modular Web Cheat Sheet

CREATE

Set Document

Firebase Doc

import { collection, doc, setDoc } from "firebase/firestore";
 
const ref = collection(db, "users");
 
await setDoc(doc(ref, "MichaelScott"), {
  role: "Regional manager",
  atOffice: true,
});

Add Document

Firebase Doc

import { collection, addDoc } from "firebase/firestore";
 
const ref = collection(db, "users");
 
await addDoc(ref, {
  role: "Npc",
  atOffice: false,
});

Add Subcollection

import { collection, addDoc } from "firebase/firestore";
 
const ref = collection(db, "users", "MichaelScott", "problems");
 
await addDoc(ref, {
  name: "corporate",
});

READ

Get Document

Firebase Doc

import { doc, getDoc } from "firebase/firestore";
 
const docSnap = await getDoc(doc(db, "users", "MichaelScott"));
 
console.log({ ...docSnap.data(), id: docSnap.id });

Get Document Realtime

Firebase Doc

import { doc, onSnapshot } from "firebase/firestore";
 
const unsub = onSnapshot(doc(db, "users", "MichaelScott"), (docSnap) => {
  console.log(docSnap.data());
});
 
// Stop listening to changes
unsub();

Get All documents

Firebase Doc

import { collection, getDocs } from "firebase/firestore";
 
const ref = collection(db, "users");
const querySnap = await getDocs(ref);
 
querySnap.forEach((docSnap) => {
  console.log(docSnap.data());
});

Get document subcollection

Firebase Doc

const { collection, getDocs } = require("firebase/firestore");
 
const ref = collection(db, "users", "MichaelScott", "problems");
const querySnap = await getDocs(ref);
 
querySnap.forEach((docSnap) => {
  console.log(docSnap.data());
});

Get all subcollections

Firebase Doc

import { collectionGroup, getDocs } from "firebase/firestore";  
 
const ref = collectionGroup(db, 'problems');
const querySnap = await getDocs(ref);
 
querySnap.forEach((docSnap) => {
    console.log(docSnap.data());
});

Query documents

Firebase Doc

import { collection, query, where, getDocs } from "firebase/firestore";
 
const ref = collection(db, "users");
const q = query(
  ref,
  where("role", "==", "Regional manager"),
  where("atOffice", "==", true)
);
const querySnap = await getDocs(q);
 
querySnap.forEach((docSnap) => {
  console.log(docSnap.data());
});

Query examples

where("population", ">", 1000000),
where("regions", "array-contains", "west_coast"),
where("regions", "array-contains-any", ["west_coast", "east_coast"]); // returns every city document where the regions field is an array that contains west_coast or east_coast
where("country", "in", ["USA", "Japan"]); //eturns every city document where the country field is set to USA or Japan
orderBy("name", "desc"); // orders responses
limit(3); // limits reponses
or(where("capital", "==", true), where("population", ">=", 1000000));
< less than
<= less than or equal to
== equal to
 
> greater than
>= greater than or equal to
!= not equal to
 
array-contains
array-contains-any
in
not-in

Fancy stuff can count() sum() and avarage() (opens in a new tab)

Paginate startAt() endAt (opens in a new tab)

UPDATE

Update Document

Firebase Doc

import { doc, updateDoc } from "firebase/firestore";
 
await updateDoc(doc(db, "users", "MichaelScott"), {
  atOffice: false,
});

Update nested object

Firebase Doc

import { doc, setDoc, updateDoc } from "firebase/firestore";
 
await updateDoc(doc(db, "users", "MichaelScott"), {
  "favorites.song": "Just Dance",
});

Update Array

Firebase Doc

import { doc, updateDoc, arrayUnion, arrayRemove } from "firebase/firestore";
 
await updateDoc(doc(db, "users", "MichaelScott"), {
  enemies: arrayUnion("Toby"),
  friends: arrayRemove("Jim"),
});

DELETE

Delete document field

import { doc, updateDoc, deleteField } from "firebase/firestore";
 
await updateDoc(doc(db, "users", "MichaelScott"), {
  enemies: deleteField(),
});

Delete document

Firebase Doc

import { doc, deleteDoc } from "firebase/firestore";
 
await deleteDoc(doc(db, "users", "MichaelScott"));
 
//Deleting a document does not delete its subcollections!

Delete collection

Deleting collections from a Web client is not recommended. Check out cloud functions or firebase admin for deleting collections.