title: How to check if a pymongo cursor has query results
body: <p>I need to check if a <code>find</code> statement returns a non-empty query.</p>
  
  <p>What I was doing was the following:</p>
  
  <pre><code>query = collection.find({"string": field})
  if not query: #do something
  </code></pre>
  
  <p>Then I realized that my <code>if</code> statement was never executed because <code>find</code> returns a cursor, either the query is empty or not.</p>
  
  <p>Therefore I checked the <a href="http://api.mongodb.org/python/current/api/pymongo/cursor.html" rel="noreferrer">documentation</a> and I find two methods that can help me:</p>
  
  <ol>
  <li><p><code>count(with_limit_and_skip=False)</code> which (from the description):</p>
  
  <blockquote>
    <p>Returns the number of documents in the results set for this query.</p>
  </blockquote>
  
  <p>It seems a good way to check, but this means that I need to count
  all the results in cursor to know if it is zero or not, right? A little bit expensive?</p></li>
  <li><p><code>retrieved</code> which (from the description):</p>
  
  <blockquote>
    <p>The number of documents retrieved so far.</p>
  </blockquote>
  
  <p>I tested it on an empty query set and it returns zero, but it's not
  clear what it does and I don't know if it's right for me.</p></li>
  </ol>
  
  <p>So, which is the best way (best practice) to check if a <code>find()</code> query returns an empty set or not? Is one of the methods described above right for this purpose? And what about performance? Are there other ways to do it?</p>
  
  <hr>
  
  <p>Just to be clear: I need to know if the query is empty and I'd like to find the best way with the cursor with respect to performance and being pythonic.</p>