我怎样才能使用蓝鸟Promiseify节点的游标和/或集合'toArray()mongodb模块?

相关软件包:

"dependencies": { "mongodb": "1.4.x", "bluebird": "2.3.x" } 

我看过:

  • 我如何使用蓝鸟promisify MongoDB原生的Javascript驱动程序?
  • 蓝鸟Promisfy.each,for循环和if语句?
  • https://stackoverflow.com/a/21733446/438992
  • 蓝鸟的promisification文件
  • 其他一些地方

我困在findAsync({})

更喜欢一个游标,但是很less有我想要调用toArray()

这也是可能的,我完全错了。

 MongoClient.connectAsync('mongodb://127.0.0.1:27017/sr') .then(function(_db) { db = _db; return db.collectionAsync('posts'); }) .then(function(colPosts) { return colPosts.findAsync({}); }) .then ( A MIRACLE OCCURS ) .catch(function(e) { console.log(e); }) .finally(function() { if (db) db.close(); }); 

在奇迹发生的地方,我想要迭代游标结果或数组集合。 我在解决如何解决这个问题方面遇到困难。

据我所知, .findAsync返回一个游标的承诺。 如果你想把数据拉到内存中(与.toArray ),我想你正在寻找的东西是这样的:

 MongoClient.connectAsync('mongodb://127.0.0.1:27017/sr') .then(function(_db) { db = _db; return db .collection('posts') .find({}) .limit(limit) .sort(sort) .toArrayAsync(); }) .then(function(posts) { console.log('posts', posts); }) .catch(function(e) { console.log(e); });