用nodejs删除mongodb中ObjectId类型数据
mongodb中"_id"下面有个ObjectId类型的数据,想通过这个数据把整个对像删除,费了半天劲终于搞定费话少说上代码1 module.exports = function (req, res) {
2 var dbCollection = req.app.db.collection('alarm.user');
3 var doc = {};
4 if (req.query) {
5 doc = req.query;
6 }
7 var BSON = require('mongodb').BSONPure;
8 var obj_id = BSON.ObjectID.createFromHexString(doc._id);
9
10 dbCollection.remove({"_id":obj_id},{safe:true},function(err,result){
11 if (err){
12 console.log(err);
13 res.end('{"error":"remove fail"}');
14 }else{
15 console.log(result);
16 res.end();
17 }
18
19 });
20
21 };
页:
[1]