我有一个上限的集合,我试图使用mongoose
尾随。当所有文档都耗尽或者根本没有文档时,tailable流会出错。
mongoose schema
var photoSchema = new Schema({
name: String,
operations: Schema.Types.Mixed,
}, {
capped: {
size: 300 * 500,
max: 500,
autoIndexId: true
}
});
实施
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")
});
在我的控制台中,我收到了MongoError: No more documents in tailed cursor
和我自己的消息processed photo stream closed
。由于mongoose与MongoDB的最新版本不兼容,我看到人们过去遇到了麻烦,但我仍然遇到这个问题。
答案 0 :(得分:2)
您的光标标记不正确,因此会被忽略。你真的只希望awaitData
有一个“大写'D'”:
var processedPhotoStream = ProcessedPhoto.find().tailable(true,
{ awaitData: true }).stream();
有效选项列在addCursorFlag
要设置的标志必须是以下之一['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);
继续提供的输出:
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 }
答案 1 :(得分:0)
当我在一个不存在的集合上创建了一个tailable游标时,我收到了这个错误。在我的情况下,我创建了一个'capped'集合,但是mongoose在模型名称的末尾添加了's'。由于集合变为复数,尾部光标正在寻找集合名称'cappeds'