Create, Read, Update and Delete Firestore (Firebase) documents in Python
Firebase Firestore is an incredible tool to easily store and retrieve your data; it supports most of today’s development languages and has a huge community providing ready to use packages.
Python is used for all kind of purposes but especially for its simple and readable syntax, its numerous math-oriented packages (for Finance, Artificial Intelligence, Deep Learning…) and its enormous community.
The combination of these two technologies provides a very efficient language to process the data and a powerful storage tool, allowing applications to get updates instantly.
First, you’ll need to install these two packages:
pip install firebase firebase_admin
Then you need to get your Google Service Account Key, commonly known as serviceAccountKey.json file, you can generate it following this tutorial.
Now let’s code! Here are all the Firebase imports required to make your CRUD operations, followed by all the commands to initiate the connection.
You’ll need to replace <DATABASE_URL> with your database URL, you can find it in your project settings, formatted in this way:
Ok, let’s talk about what’s really matter. For this example we will start with a simple example of a collection of NYSE (New York Stock Exchange) stocks:
We use the set method to create a new document with its data as the argument. You can also override an existing document with this method or choose to merge into the existing document (see Add data to Cloud Firestore).
You can either get the specify document you want by using the method get (and then to_dict to get your information as a dictionary) or get the entire collection by using the method stream (and then loop through your result).
⚠️ If you try to retrieve a Reference, you will first need to get the document where the Reference is, then use get on your retrieved Reference to finally get the referenced document.
To update, we’re going to use the update method; this method requires only the field(s) you want to update. If you want to add or remove item(s) from an array, you can either add with the Array Union method or remove with the ArrayRemove method.
⚠️ If you want to filter an array, you first need to retrieve the existing array from Firestore, filter it and send it back with the update method.
To delete a document, you can use the delete method. If you want to only delete a field, you can use the DELETE_FIELD method. If you want to delete a property in an array you can refer to the previous update section with the method ArrayRemove.
⚠️ If you want to delete documents using a where clause, you will first need to request the documents, then loop through them to proceed the deletion.
And that’s it! I hope you enjoy working with Firebase and Python, I will be making additional short practical tutorials, so stay tuned!
Please leave a comment if you encounter a specific situation not listed above, or if you find a better way to execute the commands. This article is not the only response and aim to be regularly updated.