gpt4 book ai didi

javascript - 蒙哥错误: No More Documents in Tailed Cursor

转载 作者:可可西里 更新时间:2023-11-01 10:23:06 24 4
gpt4 key购买 nike

我有一个上限集合,我正尝试使用 mongoose 对其进行尾部处理。当所有文档都用完或根本没有文档时,tailable 流会出错。

Mongoose 模式

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 不兼容而遇到麻烦,但我仍然遇到这个问题。

最佳答案

您的光标标志不正确,因此被忽略。您真的只需要带有“大写‘D’”的awaitData:

var processedPhotoStream = ProcessedPhoto.find().tailable(true, 
{ awaitData: true }).stream();

addCursorFlag 的 native 驱动程序文档中列出了有效选项

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);

不断给予的输出:

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 }

关于javascript - 蒙哥错误: No More Documents in Tailed Cursor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36704040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com