Skip to content

Instantly share code, notes, and snippets.

@vinnihoke
Created October 1, 2019 19:00
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vinnihoke/917ebc267921357a9efcd679ed54edf1 to your computer and use it in GitHub Desktop.
Save vinnihoke/917ebc267921357a9efcd679ed54edf1 to your computer and use it in GitHub Desktop.
Firestore CRUD Cheat Sheet
// Setup Firestore. Note that all of these will be asyncronous tasks and can have a .then attached. Write in a config process for Firebase. Include the necessary process.env files and instructions how to make a .env file.
***************************************************************
// Add data - C
firestore.collection("CollectionName").add({
key: value,
key: value,
})
***************************************************************
// Getting data - R
firestore.collection("CollectionName").get().then((snapshot) => {snapshot.docs.map(doc => {
console.log(doc)
})
});
!!------------------------!!
// If you require a search query
firestore.collection("CollectionName").where("key", "==", "value").get();
***************************************************************
// Update data - U
firestore.collection("CollectionName").doc(ID).update({
key: newValue
});
***************************************************************
// Deleting data - D
firestore.collection("CollectionName").doc(ID).delete()
***************************************************************
@joelmm1
Copy link

joelmm1 commented Jan 26, 2021

Please include getting data from a single document.

`var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment