【问题标题】:How to retrieve number of records returned from firestore where query in python?如何检索从firestore返回的记录数 where 在python中查询?
【发布时间】:2021-04-22 11:15:35
【问题描述】:

我试图在我的 Firebase 集合中获取查询返回的记录数,但我无法检索查询返回的记录数。

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

请有人帮助我如何获取从此查询返回的记录数。我很难处理这个。

【问题讨论】:

  • docs.len, docs.size, docs.size(), ...?

标签: python firebase google-cloud-firestore


【解决方案1】:

需要将generator转换为list

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多