title: Sort operation used more than the maximum 33.5 MB of RAM
body: <p>In Pymongo, I have the following block of code. I would need to bring in all of the data associated with my time search query into memory for further handling. To do that, I use the following block of code.</p>
  <pre><code>search_query = {&quot;time&quot; : {&quot;$lte&quot; : datetime.datetime.now()}}
  search_result = mongo_collection.find(search_query)
  ascending_search_result = search_result.sort(&quot;time&quot;, 1)
  oldest_time = ascending_search_result[0][&quot;time&quot;]
  descending_search_result = search_result.sort(&quot;time&quot;,-1)
  latest_time = descending_search_result[0][&quot;time&quot;] 
  list_search_result = list(search_result) 
  </code></pre>
  <p>It:</p>
  <ul>
  <li>Searches for the matching records</li>
  <li>Sorts them to find the start and end time points</li>
  <li>Use <code>list(search_result)</code> to bring it into memory and convert it into a list</li>
  </ul>
  <p>However, when I run <code>list(search_result)</code>, the following error is returned.</p>
  <pre><code> Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.', 'code': 96, 'codeName': 'OperationFailed'}
  </code></pre>
  <p>What can I do to resolve this error?</p>
  <p><strong>Edit</strong> I was wondering if grabbing the items in the search result individually would solve the problem. Unfortunately, this did not solve the problem. Same error thrown. I thought this would only bring in one item at a time, which would be the smallest step in importing the records.</p>
  <pre><code>list_search_result = list()
  for item in search_result: 
      list_search_result.append(item) 
  </code></pre>