Firestore Admin Cheat Sheet
CREATE
Set Document
Firebase Docawait db.collection("users").doc("DwightSchrute").set({ role: "Assistant to the Regional Manager" });
// merges does not overwrite
await db.collection("users").doc("DwightSchrute").set({ atOffice: true }, { merge: true });
Add Document
Firebase Docawait db.collection("users").add({ name: "Npc" });
Add Subcollection
await db.collection("users").doc("DwightSchrute").collection("problems").add({name: "Jim"});
READ
Get Document
Firebase Docconst docSnap = await db.collection("users").doc("DwightSchrute").get();
console.log(docSnap.data())
Get Document Realtime
Firebase Doc
const unsub = db.collection("users").doc("DwightSchrute").onSnapshot(
(docSnap) => {
console.log(docSnap.data())
},
(err) => {
console.log(err);
}
);
// Stop listening to changes
unsub()
Get all documents
Firebase Docconst querySnap = await db.collection("users").get();
querySnap.forEach((docSnap) => {
console.log({...docSnap.data(), id: docSnap.id});
});
Get document subcollection
Firebase Doc const querySnap = await db.collection("users").doc("DwightSchrute").collection("problems").get();
querySnap.forEach((docSnap) => {
console.log(docSnap.data());
});
Get All subcollections
Firebase Docconst querySnap = await db.collectionGroup('problems').get(); // get all subcollections with the name problems across all users
querySnap.forEach((docSnap) => {
console.log(docSnap.data());
});
Query documents
Firebase Docconst querySnap = await db.collection("users")
.where("atOffice", "==", true)
.where("role", "==", "Assistant to the Regional Manager")
.get();
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
.where(
Filter.or(
Filter.where('capital', '==', true),
Filter.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
UPDATE
Update Document
Firebase Docawait db.collection("users").doc("DwightSchrute").update({ atOffice: false });
Update nested object
Firebase Docawait db.collection("users").doc("DwightSchrute").update({
"favorites.song": "Kickstart My Heart",
});
Update Array
Firebase Docimport { FieldValue } from "firebase-admin/firestore"
await db.collection("users").doc("DwightSchrute").update({
friends: FieldValue.arrayRemove("Kevin"),
enemies: FieldValue.arrayUnion("Jim"),
});
DELETE
Firebase DocDelete document field
import { FieldValue } from "firebase-admin/firestore"
await db.collection("users").doc("DwightSchrute").update({
friends: FieldValue.delete(),
});
Delete document
await db.collection("users").doc("DwightSchrute").delete();
Delete document with subcollections
await db.recursiveDelete(db.collection("users").doc("DwightSchrute"));
Delete collection with subcollections
await db.recursiveDelete(db.collection("users"));