Mongojs & Express - Can't delete by id
Asked Answered
S

2

5

I am trying to delete records based on ID. I am sending the ID via the get verb and acessing it from req.params.id. My code isn't working. What am I doing wrong?

//delete user
router.get('/deleteuser/:id', function(req, res){
    var db = req.db;
    db.users.remove({'_id':req.params.id}, function(err, docs) {
        if (err) return err;
        res.send(docs); // see results
    });
});

I realize I need to add "ObjectId" before req.params.id. However, concatenating is not working. I have this so far. Is there another way:

//delete user
router.get('/deleteuser/:id', function(req, res){
    var db = req.db;
    var objId = 'ObjectId("' + req.params.id + '")';
    console.log(objId);
    db.users.remove({"_id": objId}, function(err, docs) {
  //db.users.remove({"_id": ObjectId("4d512b45cc9374271b02ec4f")});  works in mongo console
        if (err) return err;
        console.log(docs);
        res.send(docs);
    });
});

This worked:

// db.ObjectId(req.params.id)

router.get('/deleteuser/:id', function(req, res){
    var db = req.db;
    db.users.remove({"_id": db.ObjectId(req.params.id)}, function(err, docs) {  //db.users.remove({"_id": ObjectId("4d512b45cc9374271b02ec4f")});
        if (err) return err;
        console.log(docs);
        res.send(docs);
    });
});
Supplication answered 15/6, 2014 at 19:56 Comment(5)
What data type is _id? If it is an ObjectId, then you need to give it an ObjectID object, which you can build from the base64 representation with the constructor: new ObjectID(req.params.id)Jemie
Overlooking the fact that you use HTTP GET to delete an entity, which seems very evil, how do you know it isn't working? What happens? Do you get an error? Is the document still in the database after you delete it? What is the output of docs?Jemie
Thinking about the use of GET for a moment, have you considered the effects of caching? Are you sure your requests are hitting your server? A print statement may help with this.Jemie
It is hitting the server because if I delete by the firstname field it works.Supplication
It is also return n:0, so I can see it hasn't deleted anything.Supplication
S
8

I accessed it using this:

db.ObjectId(req.params.id)
Supplication answered 29/6, 2014 at 15:46 Comment(0)
V
2

By Id can be delete by passing the Id in string form , no json needs be passed.

db.users.removeById(req.params.id, function(err, docs) {

shall be the line that shall come in.

If you want to maintain the JSON syntax you nead to do something like

db.users.remove({'_id': new ObjectID(req.params.id)}, function(err, docs) {

may do it. ObjectID will depend on the driver you are using.

Vorster answered 15/6, 2014 at 20:5 Comment(4)
Neither of these worked. removeById and new Object return 'not defined.'Supplication
What does JSON have to do with this?Jemie
The OP clarified that _id is a string, not an ObjectIDJemie
github.com/kissjs/node-mongoskin#collectionremovebyidid- If you can let me know the driver you're using, i'll be able to help better. What I gave was for MongoSkin but every driver supports this as this is supported by mongoDb docs.mongodb.org/manual/applications/driversVorster

© 2022 - 2024 — McMap. All rights reserved.