var co = require('co');
var MongoClient = require('mongoskin').MongoClient;
var db = MongoClient.connect('mongodb://localhost:27017/hellye');
var countries = db.collection('countries', {
strict: true
});
co(function* () {
var doc =
yield countries.find({
country: 'scotland'
}).limit(1);
console.log('> 1');
console.log(doc);
});
countries.find({
country: 'scotland'
}).limit(1).next(function (err, doc) {
console.log('> 2');
console.log(doc);
});
上面的代码在控制台中输出以下内容:
> 2
{ _id: 56b33957a345a53d4c15f6bf,
country: 'scotland',
primaryLanguage: 'english' }
第一个查询不起作用,为什么会这样?文档说它返回一个Promise,因此它应该:https://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#find
答案 0 :(得分:0)
.toArray()
。
var doc =
yield countries.find({
country: 'scotland'
}).limit(1).toArray();