💩 firestore crud operations
I recently added a few videos to my Firebase series which cover CRUD operations in Firestore. But watching a video can get tiresome if you're looking for a snippet of code. These methods are something I'll come back to time and time again, so here's a quick copy and paste reference for CRUD operations in Firestore.
🆕 create
This snippet creates a new document in a collection. It relies on Firestore to automatically generate a document id
.
📚 read
This function retrieves all documents from a collection. It merges the id
with the document data.
This method gets a single document by id
from a collection. Similar to the function above, it combines the id
and the document data.
Many times you won't be pulling documents by id
, but rather searching them by a field name. This function gets a document by its name
field. Again, it merges the id
with the data when returning the document.
☝🏼 update
This function updates an existing document by id
in a collection. Updates are merged into the existing document. A second call to firestore
is made to retrieve the latest document. The returned object combines the id
and the document data.
If you are looking to replace a document, see the set
documentation.
⛔ delete
This function deletes a document by id
and returns that id
.
I hope this little cheat sheet provides as much value to you as it already has to me!