├── README.md ├── create_data.py ├── delete_data.py ├── read_data.py └── update_data.py /README.md: -------------------------------------------------------------------------------- 1 | # Cloud_Firestore_CRUD_Tutorials 2 | 3 | ## About 4 | This is the source code for videos in the Python Firebase Cloud Firestore CRUD series: https://www.youtube.com/playlist?list=PLs3IFJPw3G9LW-rGJ8EBMaCd8OxGm_qQe 5 | Tutorial by Youtube channel: Code First 6 | Channel: youtube.com/CodeFirstio 7 | Email: code.first.io@gmail.com 8 | You can ask me questions or just drop a line! 9 | 10 | ## What does this code do? 11 | Through this tutorial, you will learn how to: 12 | * Create a Firebase project 13 | * Initialize a Firebase app with Python 14 | * Use the Firebase Admin SDK 15 | * Insert data into the Cloud Firestore 16 | * Update data in the Cloud Firestore 17 | * Delete data from the Cloud Firestore 18 | * Query the Cloud Firestore 19 | 20 | ## Requirements 21 | Firebase Admin SDK is required. Install it with ___pip install firebase_admin___ 22 | -------------------------------------------------------------------------------- /create_data.py: -------------------------------------------------------------------------------- 1 | import firebase_admin 2 | from firebase_admin import credentials 3 | from firebase_admin import firestore 4 | 5 | # Setup 6 | cred = credentials.Certificate("serviceAccountKey.json") 7 | firebase_admin.initialize_app(cred) 8 | 9 | db=firestore.client() 10 | 11 | # Using add to add documents with auto generated keys 12 | db.collection('persons').add({'name':'John', 'age':40, 'address': "New York"}) 13 | db.collection('persons').add({'name':'Jane', 'age':50, 'address': "Los Angeles"}) 14 | db.collection('persons').add({'name':'Mark', 'age':40, 'address': "Paris"}) 15 | db.collection('persons').add({'name':'Harry', 'age':40, 'address': "London"}) 16 | db.collection('persons').add({'name':'Ron', 'age':40, 'address': "Milan"}) 17 | 18 | # Create a reference for the document before setting 19 | data = { 20 | 'name': 'Harry Potter', 21 | 'address': 'USA' 22 | } 23 | 24 | # Add a new doc in collection 'persons' with ID 'HP' 25 | db.collection('persons').document('HP').set(data) 26 | 27 | # Merge new data with existing data for 'HP' 28 | data = {'employed':True} 29 | db.collection('persons').document('HP').set(data, merge=True) 30 | 31 | # Using document() to get an auto generated ID with set() 32 | data = { 33 | 'name': 'Iron Man', 34 | 'address': 'USA' 35 | } 36 | document_reference=db.collection('heroes').document() 37 | document_reference.set(data) 38 | 39 | # Adding subcollections 40 | document_reference.collection('movies').add({'name':'Avengers'}) 41 | -------------------------------------------------------------------------------- /delete_data.py: -------------------------------------------------------------------------------- 1 | import firebase_admin 2 | from firebase_admin import credentials 3 | from firebase_admin import firestore 4 | 5 | # Setup 6 | cred = credentials.Certificate("serviceAccountKey.json") 7 | firebase_admin.initialize_app(cred) 8 | 9 | db=firestore.client() 10 | 11 | # Delete a document - known ID 12 | db.collection('persons').document("p1").delete() 13 | 14 | # Delete a field - known ID 15 | db.collection('persons').document("p2").update("age", firestore.DELETE_FIELD) 16 | 17 | # Delete a document with a known ID 18 | docs = db.collection('persons').where("age", ">=", 50).get() # Get all documents with age >=50 19 | for doc in docs: 20 | key = doc.id 21 | db.collection('persons').document(key).delete() 22 | 23 | # Delete a field - unknown ID 24 | docs = db.collection('persons').where("age", ">=", 40).get() # Get all documents with age >=40 25 | for doc in docs: 26 | key = doc.id 27 | db.collection('persons').document(key).update("age", firestore.DELETE_FIELD) 28 | 29 | # Delete all documents in a collection 30 | docs = db.collection('persons').get() # Get all data 31 | for doc in docs: 32 | db.collection('persons').document(key).delete() 33 | -------------------------------------------------------------------------------- /read_data.py: -------------------------------------------------------------------------------- 1 | import firebase_admin 2 | from firebase_admin import credentials 3 | from firebase_admin import firestore 4 | 5 | # Setup 6 | cred = credentials.Certificate("serviceAccountKey.json") 7 | firebase_admin.initialize_app(cred) 8 | 9 | db=firestore.client() 10 | 11 | # Read data 12 | # Get a document with known id 13 | result = db.collection('persons').document("p1").get() 14 | if result.exists: 15 | print(result.to_dict()) 16 | 17 | # Get all documents 18 | docs = db.collection('persons').get() 19 | for doc in docs: 20 | print(doc.to_dict()) 21 | 22 | # Query 23 | # Equal 24 | docs = db.collection('persons').where("age", "==", "52").get() 25 | for doc in docs: 26 | print(doc.to_dict()) 27 | 28 | # Greater than 29 | docs = db.collection('persons').where("age", ">", "22").get() 30 | for doc in docs: 31 | print(doc.to_dict()) 32 | 33 | # Array contains 34 | docs = db.collection('persons').where("socials", "array_contains", "facebook").get() 35 | for doc in docs: 36 | print(doc.to_dict()) 37 | 38 | # In 39 | docs = db.collection('persons').where("address", "in", ["Milan", "London"]).get() 40 | for doc in docs: 41 | print(doc.to_dict()) 42 | -------------------------------------------------------------------------------- /update_data.py: -------------------------------------------------------------------------------- 1 | import firebase_admin 2 | from firebase_admin import credentials 3 | from firebase_admin import firestore 4 | 5 | # Setup 6 | cred = credentials.Certificate("serviceAccountKey.json") 7 | firebase_admin.initialize_app(cred) 8 | 9 | db=firestore.client() 10 | 11 | # Update data with known key 12 | db.collection('persons').document("p1").update({"age": 50}) # field already exists 13 | db.collection('persons').document("p1").update({"age": firestore.Increment(2)}) # increment a field 14 | db.collection('persons').document("p1").update({"occupation": "engineer"}) # the field will be added 15 | db.collection('persons').document("p1").update({"occupation": "engineer"}) 16 | db.collection('persons').document("p2").update({"socials": firestore.ArrayRemove(['linkedin'])}) 17 | db.collection('persons').document("p1").update({"socials": firestore.ArrayUnion(['linkedin'])}) 18 | 19 | 20 | # Update data with unknown key 21 | docs = db.collection('persons').get() # Get all data 22 | for doc in docs: 23 | if doc.to_dict()["age"]>=40: # Check if age>=40 24 | key = doc.id 25 | db.collection('persons').document(key).update({"age_group":"middle age"}) 26 | 27 | # Update data with unknown key: second way 28 | docs = db.collection('persons').where("age", ">=", 40).get() # Get all documents with age >=40 29 | for doc in docs: 30 | key = doc.id 31 | db.collection('persons').document(key).update({"age_group":"middle age"}) 32 | --------------------------------------------------------------------------------