相关包:
"dependencies": {
"mongodb": "1.4.x",
"bluebird": "2.3.x"
}
我看过:
- 我如何使用蓝鸟承诺MongoDB原生Javascript驱动程序?
- 知更鸟在Promisfy。每个都有for循环和if语句?
- https://stackoverflow.com/a/21733446/438992
- 蓝鸟承诺文件
- 其他几个地方
我在findAsync({})
之后卡住了。
我更喜欢游标,但很少有太多的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);
});