Firestore DocumentSnapshot to Python dictionary

2019-08-26 06:57发布

Hi I've been going around in circles trying to get my Firestore data into a Python 2 dictionary.

    doc_ref = db.collection('things1').document('ref1').collection('things2').document('ref2')
    doc = doc_ref.get()

gets me a DocumentSnapshot - I was hoping to get a dict. What's the proper way to create a dict from the results. Tried the docs and just end up with the object. Something dumb I'm (not) doing.

Thanks

2条回答
The star\"
2楼-- · 2019-08-26 07:31

You could use to_dict(). This should give you the dictionary of the result.

doc_ref = db.collection('things1').document('ref1').collection('things2').document('ref2') 
doc = doc_ref.get().to_dict()
查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-26 07:50

This worked for me.

doc = {el.id: el.to_dict() for el in doc_ref}
查看更多
登录 后发表回答