Javascript Mongoose QueryStream.pause()不暂停?

Javascript Mongoose QueryStream.pause()不暂停?,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,节点v.4.2.3和猫鼬v.4.3.6 我必须遍历一个大型(>10k文档)集合,并处理每个文档 在阅读关于如何处理这种迭代的文档时,我偶然发现了QueryStream,我认为它可以解决我所有的问题 function progress(total, t, current) { process.stdout.clearLine(); // clear current text process.stdout.write(Math.round(t / total * 100) + '%

节点v.4.2.3和猫鼬v.4.3.6

我必须遍历一个大型(>10k文档)集合,并处理每个文档

在阅读关于如何处理这种迭代的文档时,我偶然发现了QueryStream,我认为它可以解决我所有的问题

function progress(total, t, current) {
    process.stdout.clearLine();  // clear current text
    process.stdout.write(Math.round(t / total * 100) + '% ' + t + ' / ' + total + ' ' + current);
    process.stdout.cursorTo(0);
}

function loadBalance(current, stream) {
    if(!stream.paused && current > 50) {
        log('DEBUG', 'loadBalance', 'pause');
        stream.pause();
    } else if (stream.paused && current < 10) {
        log('DEBUG', 'loadBalance', 'resume');
        stream.resume();
    }
}

var total = 0,
    error = 0,
    goods = 0,
    current = 0;

stream = Raw.find().stream();
stream.on('data', function (doc) {
    heavyProcess(doc, function (err, refined) {
        current = current + 1;
        loadBalance(current, stream);
        printP(total, goods + error, current);
        if(err) {
            error = error + 1;
            current = current - 1;
            loadBalance(current, stream);
        } else {
            new Pure(refined).save(function (err) {
                if(err) {
                    error = error + 1;
                    current = current - 1;
                    loadBalance(current, stream);
                } else {
                    goods = goods + 1;
                    current = current - 1;
                    loadBalance(current, stream);
                }
            });
        }
    });
}).on('error', function (err) {
    log('ERROR', 'stream', err);
}).on('close', function () {
    log('INFO', 'end', goods + ' / ' + total + ' ( ' + (goods/total*100) + '%) OK_');
    log('INFO', 'end', error + ' / ' + total + ' ( ' + (error/total*100) + '%) NOK');
    log('INFO', 'end', (total - goods - error) + ' missing');
});
功能进度(总计、t、当前){
process.stdout.clearLine();//清除当前文本
process.stdout.write(数学四舍五入(t/total*100)+'%'+t+'/'+total++'+current);
process.stdout.cursorTo(0);
}
功能负载平衡(当前、流){
如果(!stream.paused&¤t>50){
日志(“调试”、“负载平衡”、“暂停”);
stream.pause();
}else if(stream.paused&¤t<10){
日志(“调试”、“负载平衡”、“恢复”);
stream.resume();
}
}
var总计=0,
错误=0,
货物=0,
电流=0;
stream=Raw.find().stream();
stream.on('data',函数(doc){
重流程(文档、函数(错误、细化){
电流=电流+1;
负载平衡(电流、流);
printP(总计、货物+错误、当前);
如果(错误){
误差=误差+1;
电流=电流-1;
负载平衡(电流、流);
}否则{
新纯(精制).保存(功能(错误){
如果(错误){
误差=误差+1;
电流=电流-1;
负载平衡(电流、流);
}否则{
货物=货物+1;
电流=电流-1;
负载平衡(电流、流);
}
});
}
});
}).on('error',函数(err){
日志('ERROR','stream',err);
}).on('close',函数(){
日志('INFO','end',goods+'/'+total+'('+(goods/total*100)+'%OK');
日志('INFO','end',error+'/'+total+'('+(error/total*100)+'%NOK');
日志('INFO','end',(总计-货物-错误)+'missing');
});
loadBalance确实会被调用,并在暂停流时打印,但是
'data'
事件会继续被触发,甚至认为
流。暂停的
返回true


我是否误解了
pause()
的工作?还是我误用了QueryStream

Mongoose查询流是v1流。在文档中称为节点0.8 ReadStream()

这意味着暂停事件是“建议性的”

此处的警告意味着在调用pause后,某些数据事件仍将泄漏。
这与底层流缓存有关,是正确的流v1行为。
您必须使用调用pause后生成的任何数据事件。 从开发人员的角度来看,这种行为肯定不是最优的,这就是为什么streams v2()中对其进行了更改

这里有一个与v2查询流相关的mongoogejs问题,我不认为有任何计划在短期内实施v2查询流。

从问题中引用,这可能是解决您问题的一个方法:

var readStream = (new stream.Readable({ objectMode: true })).wrap(Model.find({}).stream());

所以,真正的问题不是我发布的代码,而是模型生成

我使用了一个新的连接,将原始链接到它,开始将纯链接到它,但在最后一刻将它链接到默认的mongoose连接:

db = mongoose.createConnection('mongodb://127.0.0.1/SNCF');          //Creer la connexion a mongodb

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {                                       //Une fois connecte

    raw = new mongoose.Schema(
        {
            //...
        },
        {
            strict: true,
            collection: 'Raw'
        }
    );

    Raw = db.model('Raw', raw, 'Raw'); //<--- OK

    pure = new mongoose.Schema(
        {
            //...
        },
        {
            strict: true,
            collection: 'Pure'
        }
    );

    Pure = mongoose.model('Pure', pure, 'Pure'); //<-- ERROR
});
db=mongoose.createConnection('mongodb://127.0.0.1/SNCF');          //蒙哥达河
db.on('error',console.error.bind(console,'connectionerror:');
db.once('open',函数(){//Une fois connecte
raw=新的mongoose.Schema(
{
//...
},
{
严格:是的,
收藏:'生'
}
);
Raw=db.model('Raw',Raw',Raw')//