var
mongoclient
=
require('mongodb').mongoclient
var
db_conn_str
=
'mongodb://localhost:27017/wilsondb1'
var
selectdata
=
function(db,
callback)
{
//连接到表
var
collection
=
db.collection('tb2')
//查询数据
var
wherestr
=
{"name":'wilson001'}
collection.find(wherestr).toarray(function(err,
result)
{
if(err)
{
console.log('error:'+
err)
return
}
callback(result)
})
}
mongoclient.connect(db_conn_str,
function(err,
db)
{
console.log("连接成功!")
selectdata(db,
function(result)
{
console.log(result)
db.close()
})
})
使用的方法很简单:var mongoDbConnection = require('./lib/connection.js')
exports.index = function(req, res, next) {
mongoDbConnection(function(databaseConnection) {
databaseConnection.collection('collectionName', function(error, collection) {
collection.find().toArray(function(error, results) {
//blah blah
})
})
})
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)