使用节点的本机mongoDB驱动程序流查询结果

bal*_*afi 2 javascript mongodb node.js

我在mongoDB集合中有100,000条记录,并尝试使用本机驱动程序在node.js应用程序中检索它们.

我在MongoDB doc中为CursorStream执行示例,但得到错误:

RangeError: Maximum call stack size exceeded
Run Code Online (Sandbox Code Playgroud)

在此错误之前,我得到了很多:

(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

var query = {...};
var fields = {...};
var options = {
    // "limit": 10
    //"skip": 10,
    //"sort": title
}

var stream = myCollection.find(query, fields, options).stream();
//  stream.pause();
var results = [];
stream.on('data', function (item){
    results.push(item);
    stream.pause();
    // Restart the stream after 1 miliscecond
    setTimeout(function() {
        stream.resume();
    }, 1);
});

stream.on('close'.....
Run Code Online (Sandbox Code Playgroud)

当我没有为'data'事件定义监听器时,也会发生错误.但是如果在创建后立即暂停流,则不会发生.

mongod版本是v2.4.1节点驱动程序版本是1.2.x.

任何帮助/提示将不胜感激.

bal*_*afi 6

看起来问题是通过在Cursor Stream中设置批量大小来解决的:

var stream = myCollection.find(query, fields, options).batchSize(10000).stream();
Run Code Online (Sandbox Code Playgroud)