title: In MongoDB how can I retrieve all documents in a collection, one chunk at a time?
body: <p>I need to process through every entry of a 1TB MonogDB collection, so I was thinking I should be able to do something like</p>
  <pre><code>const length = db.collection.count()
  const iterate = (start = 0, chunkSize = 100000) =&gt; {
      const end = start + chunkSize
      db.collection.find({}, start, end).toArray().then(items =&gt; {
          // process items
          if (end &lt; length) iterate(end)
          else console.log(&quot;all done!&quot;)
      })
  }
  iterate()
  </code></pre>
  <p>But obviously <code>collection.find()</code> doesn't take range parameters like that. Can someone explain the proper way to do this?</p>