如何知道查询是否不返回文档 [英] How to know if query doesn't return documents

查看:67
本文介绍了如何知道查询是否不返回文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I know if docs is empty? I can't do len(docs)

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

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

I need to know if there is no document that matches the query

谢谢!

推荐答案

由于stream()

Since stream() returns a generator, there won't be a trivial way to determine if it is empty without actually reading it.

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

By far the simplest solution would delay knowing until after you are past the loop. Something like this:

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")

否则,您将不得不围绕允许窥视的流的生成器构建自己的生成器.参见此问题.

Otherwise, you get into having to build your own generator around the stream's generator that allows peeking. See this question.

这篇关于如何知道查询是否不返回文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆