检查集合查询是否返回了一组文档- Firestore python



根据这里的文档来查询一组文档,我们需要使用集合查询。

执行查询后。并使用以下代码查找是否存在文档。

code_ref            = db.collection(u'col1')
code_query          = code_ref.limit(1)        
code_docs           = code_query.get()
code_docs_array = []
for elements in code_docs:
code_docs_array.append(elements)
if (len(code_docs_array) > 0):
# there are docs
else:                        
# there are no docs

我确信上面的方法是低效的。是否有与docs等价的词?在处理集合查询时存在函数?

您应该能够在查询返回的每个对象中检查'exists'。下面是我刚刚运行的一个测试:

db = firestore.client()
query = db.collection(u'projects').where(u'name', u'==', 'myname').limit(1).get()
for doc in query:
if doc.exists:
print("Document found")