【发布时间】:2016-05-31 09:47:06
【问题描述】:
var count = 0;
list.forEach(function(entry){
var coll = db.collection(entry);
coll.count(function(err,result){
count = count + result;
})
})
function(count){
//do something with count
}
我在 node.js 上使用 mongoDB 本机驱动程序,问题是应该在计算每个集合中的所有条目后使用 count 的函数会提前触发,这很明显,因为它是异步的.我已经搜索了很长一段时间的解决方案,但还没有找到任何东西。
【问题讨论】:
-
我确实只是在工作中做到了这一点。 mongo 模块是异步的,所以最好的方法是使用一个 Promise 库(或原生 Promise)。我建议 bluebird 或 async
标签: javascript node.js mongodb