【发布时间】:2017-02-27 15:09:00
【问题描述】:
我有一个包含 500k 文档的集合(集合大约需要 130mb)
我正在使用标准的 mongodb 驱动程序:
var mongodb = require('mongodb');
我正在尝试使用游标在 node.js 中遍历此集合。 (因为 .toArray 需要很长时间才能将整个数据集放入内存)
var cursor = db.collection('test').find({});
cursor.each(function(err, doc) {
// only does this 1000 times
});
我发现它只做了 1000 次,所以我查看了文档 https://mongodb.github.io/node-mongodb-native/api-generated/cursor.html 并在“每个”部分下,它说要增加批量大小。
所以我做了一个非常大的批量,我没有找到让它无限的方法。如果您知道方法,请告诉我。
var cursor = db.collection('test').find({}).batchSize(1000000000000);
cursor.each(function(err, doc) {
// only does this 30382 times
});
再增加批量大小不会使其迭代更多的元素,然后是 30382。
如何让cursor.each() 迭代 500,000 次?
【问题讨论】:
标签: javascript node.js mongodb