mongoose使用原始mongodb带auth

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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以后再进行查询