Python Firestore如何知道查询是否不返回文档

问题描述 投票:1回答:1

我怎么知道文档是否为空?我不能len(docs)

docs = query.stream()
for doc in docs:
    // do something

我需要知道是否没有与查询匹配的文档

谢谢!

python python-3.x firebase google-cloud-firestore
1个回答
0
投票
由于stream()返回一个生成器,因此不容易确定它是否为空。

到目前为止,最简单的解决方案将延迟知道,直到您经过循环。像这样的东西:

docs = query.stream() stream_empty = True for doc in docs: stream_empty = False # do something if stream_empty: print("it was empty") else: print("it wasn't empty")

否则,您将不得不围绕允许偷看的流构建自己的生成器。参见this question
© www.soinside.com 2019 - 2024. All rights reserved.