MongoSkin Promise查询不起作用

时间:2016-02-04 13:53:35

标签: node.js mongodb mongoskin

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

1 个答案:

答案 0 :(得分:0)

最后需要

.toArray()

var doc =
    yield countries.find({
        country: 'scotland'
    }).limit(1).toArray();