3.0文档:http://mongodb.github.io/node-mongodb-native/3.0/
1.引入依赖包:
npm install mongodb --save-dev
2.建立一个简单的服务,引入mongodb:
var express = require('express');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var dbURL = 'mongodb://localhost:27017';
app.listen(process.env.POST || 8080);
3.写个路由
app.get('/insert',function(req, res) {
MongoClient.connect(dbURL,function(err, db) {
assert.equal(err,null);
const person = db.db('person');
const student = person.collection('student');
student.insertOne({
"name": "insert in nodejs"
},function(error, result) {
var re = JSON.parse(result);
if (re.n === 1) {
res.send("插入成功。");
} else {
res.send("插入失败,error:" + error);
}
res.end();
db.close();
})
})
})
(1)连上数据库:connect(dbURL,callback)
(2)获取到要操作的数据库,然后获取要操作的表:
const person = db.db('person');
const student = person.collection('student');
旧版本的写法是直接获取person数据库,回调函数中的db为返回的person数据库:
var dbURL = 'mongodb://localhost:27017/person';
var student = db.collection('student');
这样写在新版中会报错: db.collection() is not a function.大概是废除了。
在浏览器中访问,然后用命令查看一下是否插入成功:
// 查
app.get('/query',function(req, res) {
MongoClient.connect(dbURL,function(err, db) {
assert.equal(err,null);
const person = db.db('person');
const student = person.collection('student');
student.find({}).toArray(function(error, docs) {
res.send(docs);
res.end();
db.close();
})
})
})
// 改
app.get('/update',function(req, res) {
MongoClient.connect(dbURL,function(err, db) {
assert.equal(err,null);
const person = db.db('person');
const student = person.collection('student');
student.updateOne({
"name": "insert in nodejs test03"
}, { $set: { "name" : "test" } }, function(error, result) {
var re = JSON.parse(result);
if (re.n === 1) {
res.send("修改成功。");
} else {
res.send("修改失败,error:" + error);
}
res.end();
db.close();
})
})
})
// 删
app.get('/delete',function(req, res) {
var query = url.parse(req.url,true).query;
var name = query.name;
MongoClient.connect(dbURL,function(err, db) {
assert.equal(err,null);
const person = db.db('person');
const student = person.collection('student');
student.deleteOne({ "name": name }, function(error, result) {
var re = JSON.parse(result);
if (re.n === 1) {
res.send("删除成功。");
} else {
res.send("删除失败,error:" + error);
}
res.end();
db.close();
})
})
})
4.封装一下:
/modules/db.js
var MongoClient = require('mongodb').MongoClient;
var dbUrl ='mongodb://localhost:27017';
function connectDb(callback) {
MongoClient.connect(dbUrl,function(err,db){
if(err) {
console.log('数据库连接失败');
return;
}
callback(db);
db.close();
})
}
exports.findOne = function(dbname,collectionname,json,callback){
connectDb(function(db) {
const DB = db.db(dbname);
const collection = DB.collection(collectionname);
var result = collection.findOne(json,null,callback);
});
}
exports.find = function(dbname,collectionname,json,callback) {
connectDb(function(db) {
const DB = db.db(dbname);
const collection = DB.collection(collectionname);
var result = collection.find(json).toArray(callback);
});
}
exports.insertOne = function(dbname,collectionname,json,callback){
connectDb(function(db) {
const DB = db.db(dbname);
const collection = DB.collection(collectionname);
collection.insertOne(json,callback)
})
}
然后在server.js中导入:
var DB = require('./modules/db.js');
使用:
app.post('/submit',function(req,res) {
var username = req.body.username;
var password = md5(req.body.password);
DB.findOne('person','userinfo',{"username":username, "password":password}, function(error, docs) {
if(docs === null) {
res.send("<script>alert('登录失败');location.href='/login'</script>");
} else {
req.session.userinfo = docs;
res.redirect('/product'); /*登录成功跳转到商品列表*/
}
});
})