代码如下

var dbUrl="mongodb://username:pw@localhost:27017/dbname"; 
var db=mongoose.createConnection(dbUrl); 
   db.on("open",function(){ 
    console.log("open"); 
       mongoose.connection.db.collection("sessions", function (err, collection) { 
           console.log("is open db ") 
           collection.find({_id: sid}).toArray(function (err, results) { 
               //console.log('session',err,results) 
               if (!results.length||err) { 
                   return false 
               } else { 
                   socket.session = JSON.parse(results[0].session) 
                   console.log(23, socket.session) 
                   next() 
               } 
           }); 
       }); 
}) 

剩下的跟mongodb原始查询语法一样. 需要在db.open以后再进行查询