title: Best way to query all documents from a mongodb collection in a reactive way w/out flooding RAM
body: <p>I want to query all the documents in a collection in a reactive way. The <a href="http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#find" rel="nofollow noreferrer">collection.find()</a> method of the mongodb nodejs driver returns a cursor that fires events for each document found in the collection. So I made this:</p>
  
  <pre><code>function giant_query = (db) =&gt; {
      var req = db.collection('mycollection').find({});   
      return Rx.Observable.merge(Rx.Observable.fromEvent(req, 'data'),
                                 Rx.Observable.fromEvent(req, 'end'),
                                 Rx.Observable.fromEvent(req, 'close'),
                                 Rx.Observable.fromEvent(req, 'readable'));
  }
  </code></pre>
  
  <p>It will do what I want: fire for each document, so I can treat then in a reactive way, like this:</p>
  
  <pre><code>Rx.Observable.of('').flatMap(giant_query).do(some_function).subscribe()
  </code></pre>
  
  <p>I could query the documents in packets of tens, but then I'd have to keep track of an index number for each time the observable stream is fired, and I'd have to make an observable loop which I do not know if it's possible or the right way to do it.</p>
  
  <p>The problem with this cursor is that I don't think it does things in packets. It'll probably fire all the events in a short period of time, therefore flooding my RAM. Even if I buffer some events in packets using Observable's buffer, the events and events data (the documents) are going to be waiting on RAM to be manipulated.</p>
  
  <p>What's the best way to deal with it n a reactive way?</p>