【问题标题】:MongoSkin Promise query not workingMongoSkin Promise 查询不起作用
【发布时间】: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

【问题讨论】:

    标签: node.js mongodb mongoskin


    【解决方案1】:

    最后需要.toArray()

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

    【讨论】:

      猜你喜欢
      • 2014-07-05
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 2015-04-18
      • 2013-09-04
      • 2011-11-29
      • 1970-01-01
      相关资源
      最近更新 更多