用nodejs删除mongodb中ObjectId类型数据

时间:2021-07-11 10:24:16

mongodb中"_id"下面有个ObjectId类型的数据,想通过这个数据把整个对像删除,费了半天劲终于搞定费话少说上代码

 module.exports = function (req, res) {
var dbCollection = req.app.db.collection('alarm.user');
var doc = {};
if (req.query) {
doc = req.query;
}
var BSON = require('mongodb').BSONPure;
var obj_id = BSON.ObjectID.createFromHexString(doc._id); dbCollection.remove({"_id":obj_id},{safe:true},function(err,result){
if (err){
console.log(err);
res.end('{"error":"remove fail"}');
}else{
console.log(result);
res.end();
} }); };