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

I've added a test collection to mongoDB, I know for a fact it's there. I know for a fact that exact query returns what I want (tested it myself in mongoHQ panel). mongo object works fine. Unfortunately the docs don't have an example, so I can't see what I'm doing wrong.

When the query runs I get a console dump of the db object:

 { db:     { databaseName: 'appxxxxxxx',
      serverConfig: 
       { host: 'staff.mongohq.com',
         port: 10096,
         options: {},
         internalMaster: true,
         connected: true,
         poolSize: 1,
         ssl: false,
         slaveOk: undefined,
dsp_099
  • 5,801
  • 17
  • 72
  • 128

1 Answers1

1

db.collection is an async call. You are accessing it in a synchronous fashion.

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

Try this:

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);
                }); 
    });
});
Rajat
  • 32,970
  • 17
  • 67
  • 87
  • 1
    That's deep. Why is it if I swap in .findOne() it works even though I use db.collection() synchronously? – dsp_099 Jul 15 '12 at 05:14