Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore - Python - Performing simple Where query always returns Query object

I know this question may sound silly, but how can I properly query data using the Firebase Admin SDK (Cloud Firestore) with Python?

Yes, I have read the docs here:

  • Perform simple and compound queries in Cloud Firestore
  • Order and limit data with Cloud Firestore

My database:

Database

My code:

existing_post = db.collection(u'posts').where(u'id', u'==', u'BpzIbkqAkk0').get()
print(existing_post)

I also tried omitting the .get() method, but also getting the same result each time.

All I've been getting is

<google.cloud.firestore_v1beta1.query.Query object at 0x10e7aab00>

Any ideas?

like image 590
Nicholas Lie Avatar asked Nov 02 '25 20:11

Nicholas Lie


2 Answers

@Gerardo's comment has lead me to the right direction. Here's the working code:

existing_posts = db.collection(u'posts').where(u'id', u'==', u'BpzIbkqAkk0').get()
for post in existing_posts:
    print(u'{} => {}'.format(post.id, post.to_dict()))
like image 194
Nicholas Lie Avatar answered Nov 04 '25 12:11

Nicholas Lie


You have to convert generator object into dictionary format using to_dict(). Then you can easily access whatever value you want.

existing_posts = db.collection(u'posts').document(u'id').get()
result=existing_posts.to_dict()
id='BpzIbkqAkk0'
result_id=result[id]

try this it may help you.

like image 35
Aditya Patil Avatar answered Nov 04 '25 10:11

Aditya Patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!