【问题标题】:Why is this query returning the database object?为什么此查询返回数据库对象?
【发布时间】:2012-07-15 04:03:41
【问题描述】:
mongo.connect(mongostr, {}, function(error, db){ 
    var mycoll = db.collection("newuser"); 
    mycoll.find({'userID':12345}, 
                {'userID':true}, 
                function(err, value) {
                    console.log(value);
                }); 
});

我已经向 mongoDB 添加了一个测试集合,我知道它就在那里。我知道确切的查询返回我想要的事实(我自己在 mongoHQ 面板中测试过)。 mongo 对象工作正常。不幸的是docs don't have an example,所以我看不出我做错了什么。

当查询运行时,我得到一个 db 对象的控制台转储:

 { db:     { databaseName: 'appxxxxxxx',
      serverConfig: 
       { host: 'staff.mongohq.com',
         port: 10096,
         options: {},
         internalMaster: true,
         connected: true,
         poolSize: 1,
         ssl: false,
         slaveOk: undefined,

【问题讨论】:

    标签: node.js mongodb heroku mongohq


    【解决方案1】:

    db.collection 是一个异步调用。您正在以同步方式访问它。

    var mycoll = db.collection("newuser");
    

    试试这个:

    mongo.connect(mongostr, {}, function(error, db){ 
        var mycoll = db.collection("newuser", function(err,collection){
                 collection.find({'userID':12345}, 
                    {'userID':true}, 
                    function(err, value) {
                        console.log(value);
                    }); 
        });
    });
    

    【讨论】:

    • 这很深。为什么如果我换入 .findOne() 即使我同步使用 db.collection() 它也能工作?
    猜你喜欢
    • 1970-01-01
    • 2016-03-21
    • 2016-11-01
    • 2021-04-29
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2018-07-12
    相关资源
    最近更新 更多