cmwSO
cmwSO

Reputation: 13

Manipulating Google Cloud Firestore data in python

I'm trying to read a specific data field in Google Cloud Firestore using python.

I have a collection called Products in my Firestore DB, and have manually added several documents with various fields.

So far I am able to pull a document using:

docs = db.collection(u'Products').where(u'checked', u'==', False).stream()
for doc in docs:
        print(u'{} => {}'.format(doc.id, doc.to_dict()))

This works, and I receive the following output:

Test2 => {'link': 'https://stackoverflow.com', 'price': 14, 'checked': False}

However, I am unable to pull out the individual 'link' string from this dict. I have tried:

print(doc.to_dict('link'))

and several iterations of this, and get the following output:

to_dict() takes 1 positional argument but 2 were given

I have been following the firebase documentation here, but have found no examples of printing fields specifically.

Any advice on how to print the 'link' string from the query I have used?

I'm running Python 3.7.

Thanks in advance for your help.

Upvotes: 1

Views: 1141

Answers (1)

robsiemb
robsiemb

Reputation: 6354

to_dict returns a dict, so your code should probably look like:

print(doc.to_dict()['link'])

instead of passing the parameter directly.

Alternatively, since you only need the one field you can try:

print(doc.get('link'))

As it avoids creating a copy of the entire snapshot.

Upvotes: 2

Related Questions