Skip to content

Instantly share code, notes, and snippets.

@heytulsiprasad
Last active February 22, 2021 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heytulsiprasad/a48a5ca34a49756e5117da90028a1649 to your computer and use it in GitHub Desktop.
Save heytulsiprasad/a48a5ca34a49756e5117da90028a1649 to your computer and use it in GitHub Desktop.
Firestore basics
// Edited this from VS code
var firebaseConfig = {};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
// Read
db.collection("cafes")
.get()
.then((snapshot) => {
snapshot.docs.forEach((doc) => console.log(doc.data()));
});
// Delete
db.collection("cafes").doc(id).delete();
// Create
db.collection("cafes").add({ name: "Me", city: "My city" });
// Search
db.collection("cafes")
.where("name", "==", "mario")
.get()
.then((snapshot) => {
snapshot.docs.forEach((doc) => console.log(doc.data()));
});
// Deep search
db.collection("cafes")
.orderBy("city")
.onSnapshot((snap) => {
let changes = snap.docChanges();
// console.log(changes);
changes.forEach((change) => {
// console.log(change.doc.data());
// console.log(change.type);
});
});
// Update
db.collection("cafes")
.doc("vsgregerr")
.update({ city: "Place where someone used to live" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment