节点大数据处理

我有500个mongodb数据库,我需要连接所有的数据库,并根据条件检查收集logging,然后logging。

一种方法是像这样使用循环和操作数据库:

for(var i=0; i<databases.length;i++){ //connect to database //find query // add record to array } 

它会正常工作,但需要很长时间。 有没有其他方法可以优化的方式进行快速处理?

至less,您可以同时运行多个查询。

看这个:

http://www.sebastianseilund.com/nodejs-async-in-practice

我需要迭代一个集合,为每个项目执行一个asynchronous任务,但是只允许同时运行x个任务,当他们完成之后,再做一些其他的事情

所以,你的代码可能会变成这样:

 var results =[]; async.forEachLimit(databases, 5, function(database, callback) { //connect to database //find query //add record to array - results.push(result) }, function(err) { //handle error here }); 

这将同时运行5 …不知道出站连接的最大数量是多less。