mongodb 数据库 增删改查

                 mongodb    数据库      增删改查

      增:

       // 引入express 模块

var express = require('express');
// 路由
var router = express.Router();
// 引入 mongodb 数据库 括号里是连接数据库的路径
var users=require('../mgs/mgs')
// =====================================
router.get('/', function(req, res, next){
console.log(req.query)
users.create(req.query,function(err,result){
if(err){console.log('写入出错')}
else{
console.log('写完=',result)
res.send(result)
}
})
});

// 导出
module.exports = router;

       

删:

var express = require('express');
var router = express.Router();

var users=require('../mgs/mgs')

// ====================================================

router.get('/', function(req, res, next) {
console.log(req.query)
// if(typeof(req.query.habit)==){req.query.habit=req.query.habit.join()}
console.log(req.query)
users.deleteOne(req.query,function(err,result){
if(err){console.log('删除出错')}
else{
console.log('删除=',result)
res.send(result)
}
})
});


module.exports = router;

 

改:

var express = require('express');
var router = express.Router();

var users=require('../mgs/mgs')


// ==============================


router.get('/', function(req, res, next) {
console.log(req.query)
users.updateOne({_id:req.query._id},req.query,function(err,result){
if(err){console.log('改出错')}
else{
console.log('改==',result)
res.send(result)
}
})
});

 

查:

var express = require('express');
// var users = require('../mgs/mgs');
var router = express.Router();
var users=require('../mgs/mgs')

// =====================================

router.get('/', function(req, res, next) {
console.log(req.query)
users.find({sex:new RegExp(req.query.sex)},function(err,result){
if(err){console.log('查询出错')}
else{
console.log('查询成功')
res.send(result)
}
})


});

module.exports = router;
posted @ 2019-12-19 20:37  柚子小哥哥  阅读(270)  评论(0编辑  收藏  举报