I have careted an application that interacts with MongoDb using NodeJS (Express JS). I am trying to remove a document using the "_id" (the one generated by MongoDB). The following piece of code just logs "Deleted Successfully", but does not actuall remove the record:
app.post('/TaskDone', function (req, res) {
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db;
var server = new Server('localhost', 27017, { auto_reconnect: true });
var database = new Db('pending-list-2', server);
database.open(function (err, db) {
if (!err) {
console.log("Connected for Deletion");
db.collection('tasks', function (err, coll) {
var str = "{_id :"+"ObjectId(" + "\"" + req.body + "\"" + ")" + "}";
console.log(str);
coll.remove(str, function (err) {
if (err) console.log(err);
else console.log("Deleted successfully");
}
);
});
}
});
});
If I use the MongoDB client and just run db.tasks.remove({_id:ObjectID("idhere")}) , it works. Is there something wrong with the express js code that I have written. I have tried a lot of things but nothing seems to work.
coll.removeshould work, but I am positive it is far from being efficient even if it doesreq.bodybe equal to the_idof your object?