10

我想扫描整个 mongo 集合并计算custom聚合。我正在使用带有猫鼬的节点。为了扫描我正在使用的整个表MyModel.find({}, callback);

当我运行代码时,我发现 mongoose 执行查询并将所需的记录收集到一个数组中,然后简单地将整个数组传递给回调。现在在完整的集合扫描中需要大量时间。

是不是我得到了一个游标对象,我可以从中迭代不断地将所需记录映射到某个回调,而不是等待一大堆被收集到数组中。(这是我观察到的,如果我错了请纠正)。

另外,有人可以建议对自定义聚合进行完整的集合扫描是否正确,或者我应该研究map-reduce或类似的替代方法。

4

1 回答 1

13

Your first option should be to use the aggregate method instead of find to do whatever aggregation you're looking to do. If that doesn't do what you need, look into mapReduce, like you mentioned.

However, if you find that you do need to iterate over a large collection, you should use Mongoose's support for streaming the result of the query rather than getting it in one big array.

var stream = MyModel.find().stream();

stream.on('data', function (doc) {
  // do something with the mongoose document
}).on('error', function (err) {
  // handle the error
}).on('close', function () {
  // the stream is closed
});
于 2013-02-14T05:02:30.023 回答