简体   繁体   中英

How to retrieve number of records returned from firestore where query in python?

I am trying to get the number of records returned by a query in my Firebase collection but I cannot retrieve the number of records returned from the query.

from google.cloud import firestore as cloud_firestore
firestore = cloud_firestore.Client()
posts_ref = firestore.collection('posts')
docs = posts_ref.where('slug', '==', post['slug']).stream()  # The is an iterable which can be looped over
# This throws an error; TypeError: object of type 'generator' has no len()
if len(docs) != 0:
    print("Post already exists!")
    exit()

# add_post_to_db()...

Please someone help me how to get the number of records returned from this query. I'm having a hard time with this.

Need to convert generator to list

docs = list(posts_ref.where('slug', '==', post['slug']).stream())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM