title: pymongo.errors.CursorNotFound: cursor id '…' not found at server
body: <p>I am trying to read about 1M documents from mongodb to csv file using pymongo. My code looks like:</p>
  
  <pre><code>import csv
  from pymongo import MongoClient
  from datetime import datetime
  from bson import json_util
  from tempfile import NamedTemporaryFile
  
  client = MongoClient('mongodb://login:pass@server:port')
  db = client.some_mongo_database
  collection = db.some_mongo_collection
  
  fromDate = datetime.strptime("2018-05-15 21:00", '%Y-%m-%d %H:%M')
  tillDate = datetime.strptime("2018-05-16 21:00", '%Y-%m-%d %H:%M')
  query = {
          "$or": [
                   {"LastUpdated": {"$gte": fromDate
                                  , "$lt": tillDate}
                   },
                   {"$and": [
                              {"Created": {"$gte": fromDate
                                         , "$lt": tillDate}
                              },
                              {"LastUpdated": None}
                         ]
                    }
              ]
          }
  
  cursor = collection.find(query, no_cursor_timeout=True)
  </code></pre>
  
  <p>after that if I'l do:</p>
  
  <pre><code>for row in cursor:
      print(row)
  cursor.close()
  </code></pre>
  
  <p>everything works fine, and I could get all the documents.
  But if I do something like that:</p>
  
  <pre><code>with NamedTemporaryFile("w", delete=False) as temp:
      csv_writer = csv.writer(temp, delimiter='\t', quotechar='\b', quoting=csv.QUOTE_MINIMAL)
      for row in cursor:
          csv_row = [ [[row['_id']], str(json.dumps(row,default=json_util.default))] ]
          csv_writer.writerows(csv_row)
  cursor.close()
  </code></pre>
  
  <p>After about 2 minutes and 200k documents I'm receive:</p>
  
  <pre><code>Traceback (most recent call last):
    File "mongo_data_loader.py", line 25, in &lt;module&gt;
      for row in cursor:
  
    File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 1169, in next
      if len(self.__data) or self._refresh():
    File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 1106, in _refresh
      self.__send_message(g)
    File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 975, in __send_message
      helpers._check_command_response(first)
    File "/Library/Python/2.7/site-packages/pymongo/helpers.py", line 142, in _check_command_response
      raise CursorNotFound(errmsg, code, response)
  pymongo.errors.CursorNotFound: cursor id 184972541202 not found
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>Python 2.7.10
  pymongo 3.6.1
  mongo db.version() 3.6.5
  </code></pre>