Reputation: 9149
I have a capped collection that I'm trying to tail using mongoose
. The tailable stream errors out when all documents are exhausted or if there are none at all.
mongoose schema
var photoSchema = new Schema({
name: String,
operations: Schema.Types.Mixed,
}, {
capped: {
size: 300 * 500,
max: 500,
autoIndexId: true
}
});
implementation
var RawPhoto = mongoose.model('RawPhoto', photoSchema);
var ProcessedPhoto = mongoose.model('ProcessedPhoto', photoSchema)
var ProcessedPhoto = mongoose.model('ProcessedPhoto', photoSchema)
var processedPhotoStream = ProcessedPhoto.find().tailable(true,
{awaitdata: true, numberOfRetries: Number.MAX_VALUE}).stream();
processedPhotoStream.on('data', function(photo){
console.log(photo.name);
}).on('error', function(error){
console.error(error);
}).on('close', function(){
console.log("processed photo stream closed")
});
In my console I get a MongoError: No more documents in tailed cursor
and my own message processed photo stream closed
. I saw that people were having trouble with this in the past due to an incompatibility between mongoose and the latest version of MongoDB, but I am still having this issue.
Upvotes: 2
Views: 1621
Reputation: 890
I got this error when i created a tailable cursor on a collection that didnt exist. In my case i created a collection 'capped' but mongoose adds the 's' at the end of model names. Because collections become plural the tailed cursor was looking for collection name 'cappeds'
Upvotes: 1
Reputation: 151122
Your cursor flags are incorrect and are therefore ignored. You really only want awaitData
with a "capital 'D'":
var processedPhotoStream = ProcessedPhoto.find().tailable(true,
{ awaitData: true }).stream();
The valid options are listed in the native driver documentation for addCursorFlag
The flag to set, must be one of following ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'partial'].
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/capped');
mongoose.set('debug',true);
var photoSchema = new Schema({
"name": String
}, {
"capped" : {
"size": 500 * 1024,
"max": 500
}
});
var Photo = mongoose.model('Photo',photoSchema);
var stream = Photo.find().tailable(true,
{ awaitData: true }).stream();
stream.on('data',function(data) {
console.log(data);
});
stream.on('err',function(err) {
console.error(err);
});
stream.on('close',function() {
console.log('done');
});
setInterval(function() {
Photo.create({ "name": "Ted" },function(err,photo) {
});
},2000);
The output that keeps on giving:
Mongoose: photos.find({}) { awaitData: true, tailable: true, fields: undefined }
Mongoose: photos.insert({ name: 'Ted', _id: ObjectId("5715af20daa62598059adaf1"), __v: 0 })
{ __v: 0, name: 'Ted', _id: 5715af20daa62598059adaf1 }
Mongoose: photos.insert({ name: 'Ted', _id: ObjectId("5715af23daa62598059adaf2"), __v: 0 })
{ __v: 0, name: 'Ted', _id: 5715af23daa62598059adaf2 }
Mongoose: photos.insert({ name: 'Ted', _id: ObjectId("5715af25daa62598059adaf3"), __v: 0 })
{ __v: 0, name: 'Ted', _id: 5715af25daa62598059adaf3 }
Upvotes: 2