Townheadbluesboy
Townheadbluesboy

Reputation: 385

Firestore DocumentSnapshot to Python dictionary

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

Upvotes: 6

Views: 15145

Answers (2)

ellaRT
ellaRT

Reputation: 1366

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()

Upvotes: 22

hellabarger
hellabarger

Reputation: 121

This worked for me.

doc = {el.id: el.to_dict() for el in doc_ref}

Upvotes: 1

Related Questions