Search code examples
node.jsmongodbnode-mongodb-native

MongoError: cursor is dead (mongo node driver)


I am using node-mongodb-native 2.0 http://mongodb.github.io/node-mongodb-native/2.0/

With the following Node.js code:

var MongoClient = require('mongodb').MongoClient;
var mongoUrl = 'mongodb://localhost/twitter';

MongoClient.connect(mongoUrl, function(err, db) {
  if (err) return console.error(err);

  var collection = db.collection('tweets');

  collection.find().limit(1000).forEach(function(tweet) {
    console.log(tweet.id);
  }, function(err) {
    if (err) console.error(err);
    db.close();
  });
});

When I set the limit to 1000 (collection.find().limit(1000)), I was able to retrieve first several hundreds records, but I got error message { [MongoError: cursor is dead] name: 'MongoError', message: 'cursor is dead' } later on (I got 1 million records in my collection). But the program runs OK we I specify 800 as limit. It's also OK to not specify any limit (just collection.find()), and the script just keeps going without any error (reading way more records than 1000).

What's wrong? How to solve? How to solve if I still want to use forEach on a cursor?


Solution

  • I have reproduced this issue with a sample data set of smaller documents. The telling part is likely this log line from MongoDB:

    2014-10-21T18:50:32.548+0100 [conn50] query twitter.tweets planSummary: COLLSCAN cursorid:30362014860 ntoreturn:200000 ntoskip:0 nscanned:199728 nscannedObjects:199728 keyUpdates:0 numYields:0 locks(micros) r:120400 nreturned:199728 reslen:4194308 120ms
    

    The key piece, I suspect is reslen:4194308 which looks suspiciously close to the default batch size of 4MiB. I've been in touch with the node.js driver developers, will let you know if this ends up as a bug (update: opened NODE-300, and confirmed fix in mongodb-core@1.0.4 as result).

    In the meantime, I'd recommend using the workaround from the comments, namely using a projection to reduce the amount of data in the results and sidestepping the issue.

    Updated Resolution: If you are seeing this (or a similar) issue, then please update your mongodb-core version and retry - the new version no longer errors and runs quite a bit faster also. I did this by removing my node_modules and re-running npm install for my test app.