How to know if query doesn't return documents

空扰寡人 提交于 2020-01-11 12:13:08

问题


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

Thank you!


回答1:


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.



来源:https://stackoverflow.com/questions/58838764/how-to-know-if-query-doesnt-return-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!