-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreate_data.py
More file actions
40 lines (32 loc) · 1.28 KB
/
Copy pathcreate_data.py
File metadata and controls
40 lines (32 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
# Setup
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db=firestore.client()
# Using add to add documents with auto generated keys
db.collection('persons').add({'name':'John', 'age':40, 'address': "New York"})
db.collection('persons').add({'name':'Jane', 'age':50, 'address': "Los Angeles"})
db.collection('persons').add({'name':'Mark', 'age':40, 'address': "Paris"})
db.collection('persons').add({'name':'Harry', 'age':40, 'address': "London"})
db.collection('persons').add({'name':'Ron', 'age':40, 'address': "Milan"})
# Create a reference for the document before setting
data = {
'name': 'Harry Potter',
'address': 'USA'
}
# Add a new doc in collection 'persons' with ID 'HP'
db.collection('persons').document('HP').set(data)
# Merge new data with existing data for 'HP'
data = {'employed':True}
db.collection('persons').document('HP').set(data, merge=True)
# Using document() to get an auto generated ID with set()
data = {
'name': 'Iron Man',
'address': 'USA'
}
document_reference=db.collection('heroes').document()
document_reference.set(data)
# Adding subcollections
document_reference.collection('movies').add({'name':'Avengers'})