Skip to content

Instantly share code, notes, and snippets.

@nileshtrivedi
Created June 18, 2018 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nileshtrivedi/c9827ae350ea3c044a2d6a5afab4c3d1 to your computer and use it in GitHub Desktop.
Save nileshtrivedi/c9827ae350ea3c044a2d6a5afab4c3d1 to your computer and use it in GitHub Desktop.
Cloud Firestore - Python example
# 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