title: Warning from mongodb - "(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate..."
body: <p>H there,</p>
  
  <p>I am running node 0.10.24 and I'm trying to get all the records from a mongodb collection and once my collection goes over 1000 elements I get this error:</p>
  
  <blockquote>
    <p>node.js:375
            throw new Error(msg);
                  ^
    Error: (node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
        at maxTickWarn (node.js:375:15)
        at process.nextTick (node.js:480:9)
        at CursorStream._next (/var/www/html/node_modules/mongodb/lib/mongodb/cursorstream.js:76:11)
        at CursorStream._onNextObject (/var/www/html/node_modules/mongodb/lib/mongodb/cursorstream.js:98:8)
        at /var/www/html/node_modules/mongodb/lib/mongodb/cursorstream.js:78:12
        at Cursor.nextObject (/var/www/html/node_modules/mongodb/lib/mongodb/cursor.js:540:5)
        at /var/www/html/node_modules/mongodb/lib/mongodb/cursorstream.js:77:18
        at process._tickCallback (node.js:415:13)</p>
  </blockquote>
  
  <p>I tried 2 ways of fetching data from mongodb without success.</p>
  
  <p>Version 1:</p>
  
  <pre><code>exports.findAll = function(req, res) {
  db.collection('products', function(err, collection) {
      var first = true;
  
      var stream = collection.find().batchSize(10000).stream();
      var results = [];
  
      stream.on('data', function(item){
          results.push(item);
      }).on('error', function (err) {
          // handle the error
          console.log(err);
      }).on('close', function () {
          // the stream is closed, so complete the response with res.end();
          res.send(results);
      });
  });
  };
  </code></pre>
  
  <p>Version 2:</p>
  
  <pre><code>var sys = require('sys'),
  http = require('http'),
  mongodb = require('mongodb');
  
  var server = new mongodb.Server("localhost", 27017, {});
  var db = new mongodb.Db('productdb', server, {});
  var client  = null
  
  db.open(function (error, clientParam) {
      if (error) {
              db.close();
              throw error;
      }
      client = clientParam;
  });
  
  http.createServer(function(req, res) {
      var collection = new mongodb.Collection(client, 'products');
  
      collection.find({}, {}, function(err, cursor) {
              if(err == null){
               cursor.toArray(function(err, docs){
                       res.writeHead(200, {'Content-Type': 'application/json'});
                       res.write(docs);
                       res.end();
                       //console.log(docs);
               });
              }else{
                      res.writeHead(200, {'Content-Type': 'text/html'});
                  res.write('An error occurred!');
                  res.end();
                  throw err;
              }
      });
      }).listen(8088);
  </code></pre>
  
  <p>In version 3 when i add a stream pause and after a timeout i resume it, I will get the result that i want, but with a significant delay.</p>
  
  <p>Version 3:</p>
  
  <pre><code>exports.findAll = function(req, res) {
  db.collection('products', function(err, collection) {
      var first = true;
  
      var stream = collection.find().batchSize(10000).stream();
      var results = [];
  
      stream.on('data', function(item){
          results.push(item);
          stream.pause();
  
          setTimeout(function(){
              stream.resume();
          }, 1);
      }).on('error', function (err) {
          // handle the error
          console.log(err);
      }).on('close', function () {
          // the stream is closed, so complete the response with res.end();
          res.send(results);
      });
  
  });
  };
  </code></pre>
  
  <p>What is the correct way to read a lot of rows, very fast from mongodb using node.js without encountering this issue?</p>
  
  <p>Thank you.</p>
  
  <p>UPDATE:</p>
  
  <p>I have found another way that still does not work, but this time it gives me this warning after the 500th row returned:</p>
  
  <pre><code>[WARNING] streamRecords method is deprecated, please use stream method which is much faster
  </code></pre>
  
  <p>And the code for this is:</p>
  
  <pre><code>db.collection('products', function(err, collection) {
  
      var stream = collection.find().streamRecords();
      var results = [];
  
      stream.on('data', function(item){
          results.push(item);
      }).on('error', function (err) {
          // handle the error
          console.log(err);
      }).on('end', function () {
          // the stream is closed, so complete the response with res.end();
          res.send(results);
      });
  });
  </code></pre>
  
  <p>If I change the streamRecords() call to stream(), I am back to my initial problem in Version 1. If I leave it this way i only get the first 500 row. :|</p>