an error about tailable cursor for a query in mongoose an error about tailable cursor for a query in mongoose mongoose mongoose

an error about tailable cursor for a query in mongoose


That doesn't look like a correct usage of a capped collection or tailable stream. But perhaps a little code first to demonstrate a working example:

var async = require('async'),    mongoose = require('mongoose'),    Schema = mongoose.Schema;var userSchema = new Schema({  email: String,},{  capped: 2048});var User = mongoose.model( "User", userSchema );mongoose.connect('mongodb://localhost/atest');var stream;async.waterfall(  [    function(callback) {      var user = new User({ email: "existing" });      user.save(function(err,doc) {        if (err) throw err;        callback();      });    },    function(callback) {      User.find({}).sort({ "$natural": -1 }).limit(1).exec(function(err,docs) {        if (err) throw err;        console.log( docs );        callback(err,docs[0]);      });    },    function(doc,callback) {      stream = User.find({ "_id": { "$gt": doc._id } }).tailable().stream();      stream.on("data",function(doc) {        console.log( "Streamed:\n%s", doc );      });      callback();    },    function(callback) {      async.eachSeries(        ['one','two','three'],        function(item,callback) {          var user = new User({ email: item });          user.save(function(err,doc) {            if (err) throw err;            console.log( "Saved:\n%s", doc );            callback();          });        },        function(err) {          if (err) throw err;          callback();        }      );    }  ]);

First things first, there really needs to be something in the capped collection for anything to work. This presumes that the collection does not exist and it is going to be initialized as a capped collection. Then the first step is making sure something is there.

Generally when you want to "tail", you just want the new documents that are inserted to appear. So before setting up the tailable cursor you want to find the "last" document in the collection.

When you know the last document in the collection, the "tailable stream" is set up to look for anything that is "greater than" that document, which are the new documents. If you did not do this, your first "data" event on the stream would empty all of the current collection items. So the options to .sort() and .limit() here do not apply. Tailing cursors initialize and "follow".

Now that the streaming interface is set up and an listener established, you can add items to the stream. These will then log accordingly, yet as this is "evented", there is no particular sequence to the logging here as either the "save" or the "stream" data event may actually fire first.


Now onto your implementation. These two lines stand out:

   , subscriptions          : [ SubscriptionSchema ]   , accessTokens           : [ AccessToken ]

Those contain embedded arrays, they are not "external" documents in another collection, even though it would not matter if it even did.The general problem here is that you are (at least in some way) introducing an array, which seems to imply some concept of "growth".

Unless your intention is to never "grow" these arrays and only ever insert the content with the new document and never update it, then this will cause problems with capped collections.

Documents in capped collections cannot "grow" beyond their initial allocated size. Trying to update where this happens will result in errors. If you think you are going to be smart about it and "pad" your documents, then this is likely to fail when replicated secondary hosts "re-sync". All documented with capped collections.