Created
June 18, 2018 06:22
-
-
Save nileshtrivedi/c9827ae350ea3c044a2d6a5afab4c3d1 to your computer and use it in GitHub Desktop.
Cloud Firestore - Python example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script can be used standalone | |
# So it doesn't depend on filesystem or Google Cloud for the credentials | |
from google.cloud import firestore | |
from google.oauth2 import service_account | |
key = 'your_credentials_json_string' | |
info = json.loads(key) | |
creds = service_account.Credentials.from_service_account_info(info) | |
db = firestore.Client(credentials=creds, project="project_name_goes_here") | |
# Upsert a document | |
doc_ref = db.collection(u'collection_name').document(u'document_id') | |
doc_ref.set({'attr': 'value'}) | |
# Fetch all docs and print | |
collection_ref = db.collection(u'd') | |
docs = collection_ref.get() | |
for doc in docs: | |
print(u'{} => {}'.format(doc.id, doc.to_dict())) | |
# AFAIK, Python lib doesn't support real-time listening for document updates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment