Last active
February 22, 2021 17:15
-
-
Save heytulsiprasad/a48a5ca34a49756e5117da90028a1649 to your computer and use it in GitHub Desktop.
Firestore basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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