【发布时间】:2023-04-05 08:55:01
【问题描述】:
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
【问题讨论】: