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

时间:2013-04-18 10:35:57

标签: javascript node.js mongodb

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

我按照MongoDB doc for CursorStream中的示例操作,但收到错误:

RangeError: Maximum call stack size exceeded

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

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

这是我的代码:

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'.....

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

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

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

2 个答案:

答案 0 :(得分:6)

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

var stream = myCollection.find(query, fields, options).batchSize(10000).stream();

答案 1 :(得分:0)

您可能希望尝试setImmediate而不是setTimeout,以确保可以刷新未完成的IO操作。另请参阅https://github.com/ZJONSSON/streamz/issues/1

相关问题