Skip to content

Instantly share code, notes, and snippets.

@MksYi
Last active August 14, 2018 09:17
Show Gist options
  • Save MksYi/e0b48b3baf866d64ab5f3fa10b360edc to your computer and use it in GitHub Desktop.
Save MksYi/e0b48b3baf866d64ab5f3fa10b360edc to your computer and use it in GitHub Desktop.
Firebase 讀取測試
#!/usr/bin/python3
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate("stust-game-firebase-adminsdk-z8f9e-565f1cf637.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
#Get Collection
doc_ref = db.collection('Q1', 'DOC1', 'Q2')
docs = doc_ref.get()
for doc in docs:
print(u'{} => {}'.format(doc.id, doc.to_dict()))
#Get Doc
doc_ref = db.collection('Q1', 'DOC1', 'Q2').document('DOC2')
docs = doc_ref.get()
print('姓名 => {}'.format(doc.to_dict()['姓名']))
print('年紀 => {}'.format(doc.to_dict()['年紀']))
print('工作 => {}'.format(doc.to_dict()['工作']))
print('Done')
"""
$ python Firebase-Read.py
DOC2 => {'年紀': '23', '工作': '魔法師', '姓名': '劉德華'}
姓名 => 劉德華
年紀 => 23
工作 => 魔法師
Done
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment