var db = require("../modules/db");
var mongodb = require("mongodb");
// 添加
module.exports.postAdd = function (req,res) {
var obj = req.body;
console.log(obj);
obj.time = new Date().getTime();
obj.age = +obj.age;
db.insertOne(res, obj, "case1113", function (err,result,db1) {
if (err) {
res.send({code:404,msg:"添加失败"})
} else {
res.send({ code: 200, msg: "添加成功" })
db1.close();
}
})
}
// 渲染
module.exports.getList = function (req, res) {
var obj = req.query;
console.log(obj); // { sort: { age: '1' } }
db.find(res, obj,"case1113", function (err, result, db1) {
if (err) {
res.send({ code: 404, msg: "添加失败" })
} else {
res.send({ code: 200, msg: "添加成功", info:result})
db1.close();
}
})
}
// 删除
module.exports.getDel = function (req, res) {
var id = req.query.id;
var obj = {
_id:mongodb.ObjectId(id)
}
db.deleteOne(res, obj,"case1113", function (err, result, db1) {
if (err) {
res.send({ code: 404, msg: "添加失败" })
} else {
res.send({ code: 200, msg: "添加成功", info: result })
db1.close();
}
})
}
// 全部删除
module.exports.getDelAll = function (req, res) {
db.drop(res, "case1113", function (err, result, db1) {
if (err) {
res.send({ code: 404, msg: "添加失败" })
} else {
res.send({ code: 200, msg: "添加成功", info: result })
db1.close();
}
})
}
// 总条数获取
module.exports.getCount = function (req, res) {
var obj = req.query;
db.count(res, obj, "case1113", function (count, db1) {
res.send({ code: 200, msg: "数据查找总条数成功", count });
db1.close();
})
}
// 渲染
module.exports.getTop = function (req, res) {
db.find(res, {}, "case1113", function (err, result, db1) {
if (err) {
res.send({ code: 404, msg: "添加失败" })
} else {
res.send({ code: 200, msg: "添加成功", info: result })
db1.close();
}
})
}
mongodb
最新推荐文章于 2024-09-03 16:41:45 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)