Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-479

Cursor.next doesn't behave as deprecated Cursor.nextObject for tailable cursors

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.0.34
    • Affects Version/s: 2.0.33
    • Component/s: None
    • Environment:
      node v0.10.33
      MongoDB v2.6.3
      Amazon Linux

      Sorry, but couldn't find specific information about differences between Cursor.next() and Cursor.nextObject() for version 2.0 regarding tailable cursors. From the documentation I was expecting that they would behave the same, being only a method rename. What happens is that Cursor.next() constantly returns "MongoError: No more documents in tailed cursor". Well, seems to me that this turns it into a normal cursor, which instead of returning a null item, returns an error. If I set the awaitData option, it waits for some data, but still returns the same error after awhile.

      Maybe I'm supposed to use it in a different way, maybe change some timeout?

      The following code illustrates the problem:

      tailable.js
      var mongodb = require('mongodb')
      mongodb.connect('mongodb://localhost:27017/test', function(err, db) {
        if (err) throw err;
      
        function getCollection (name, cb) {
          db.collection(name, { strict: true }, function(err, collection) {
            if (err || !collection) {
              db.createCollection(name, {
                size: 1024*1024,
                capped: true,
                autoIndexId: true
              }, cb);
            } else {
              cb(null, collection);
            }
          });
        }
      
        getCollection('tailable_test', function (err, tailable_test) {    
          if (err) throw err;
          tailable_test.insertOne({}, {w:1}, function (err, result) {
            if (err) throw err;
            var _id = result.ops[0]._id;
            console.log('last _id:', _id);
            var cursor = tailable_test.find({
              _id: { $gt: _id }
            }, {
              tailable: true,
              numberOfRetries: Number.MAX_VALUE,
              tailableRetryInterval: 200
            });
      
            var cursorNextObject = cursor.nextObject;
            var cursorNext = cursor.next;
            var count = 0;
            var method;
            function next() {
              if (count++ < 5) {
                cursor.nextObject(function(err, item) {
                  console.log('old nextObject call,', 'error:', err, 'item:', item, 'ts:', new Date().toJSON(), 'count:', count);
                  setTimeout(function () {next()}, 1000)
                });
              } else if (count++ < 20) {
                cursor.next(function(err, item) {
                  console.log('new next call,', 'error:', err, 'item:', item, 'ts:', new Date().toJSON(), 'count:', count);
                  setTimeout(function () {next()}, 1000)
                });
              } else {
                process.exit(0);
              }
            }
            next();
            setInterval(function () {
              tailable_test.insertOne({},{w:0});
            }, 3000);
          });
        });
      })
      
      Unable to find source-code formatter for language: title:output. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      last _id: 556871a19107ccbc136f7fa8
      old nextObject call, error: null item: { _id: 556871a49107ccbc136f7fa9 } ts: 2015-05-29T14:03:16.325Z count: 1
      old nextObject call, error: null item: { _id: 556871a79107ccbc136f7faa } ts: 2015-05-29T14:03:19.383Z count: 2
      old nextObject call, error: null item: { _id: 556871aa9107ccbc136f7fab } ts: 2015-05-29T14:03:22.350Z count: 3
      old nextObject call, error: null item: { _id: 556871ad9107ccbc136f7fac } ts: 2015-05-29T14:03:25.423Z count: 4
      old nextObject call, error: null item: { _id: 556871b09107ccbc136f7fad } ts: 2015-05-29T14:03:28.485Z count: 5
      new next call, error: { [MongoError: No more documents in tailed cursor]
        name: 'MongoError',
        message: 'No more documents in tailed cursor',
        tailable: true,
        awaitData: undefined } item: undefined ts: 2015-05-29T14:03:29.488Z count: 7
      new next call, error: { [MongoError: No more documents in tailed cursor]
        name: 'MongoError',
        message: 'No more documents in tailed cursor',
        tailable: true,
        awaitData: undefined } item: undefined ts: 2015-05-29T14:03:30.492Z count: 9
      new next call, error: null item: { _id: 556871b39107ccbc136f7fae } ts: 2015-05-29T14:03:31.495Z count: 11
      new next call, error: { [MongoError: No more documents in tailed cursor]
        name: 'MongoError',
        message: 'No more documents in tailed cursor',
        tailable: true,
        awaitData: undefined } item: undefined ts: 2015-05-29T14:03:32.498Z count: 13
      new next call, error: { [MongoError: No more documents in tailed cursor]
        name: 'MongoError',
        message: 'No more documents in tailed cursor',
        tailable: true,
        awaitData: undefined } item: undefined ts: 2015-05-29T14:03:33.501Z count: 15
      new next call, error: null item: { _id: 556871b69107ccbc136f7faf } ts: 2015-05-29T14:03:34.504Z count: 17
      new next call, error: { [MongoError: No more documents in tailed cursor]
        name: 'MongoError',
        message: 'No more documents in tailed cursor',
        tailable: true,
        awaitData: undefined } item: undefined ts: 2015-05-29T14:03:35.510Z count: 19
      

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            lmalheiro@vivocha.com Luis Malheiro
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: