4

I want to update a single record by _id in a collection in mongoDB.

UPDATE: I changed the res to req (thanks!) and implemented the db.ObjectId() around the objectId I am passing in and now I get a 500 internal server error.

    "_id" : ObjectId("54d5296711436278137af74b"),
    "username" : "alex",
    "email" : "alex@gmail",
    "fullname" : "alex man",
    "age" : "15",
    "location" : "minneap",
    "gender" : "mal"

This is my ajax call from the client.

    $.ajax({
                    type: 'PUT',
                    data: updatedUser,
                    url: '/users/updateuser/' + globalUserID,
                    dataType: 'JSON'
                }).done(function(response){

This is the routing code.

/*
* PUT to updateuser
*/
router.put('/updateuser/:id', function(req, res) {
var db = req.db;
var userToUpdate = req.params.id;
db.collection('userlist').update(
{ _id: userToUpdate},
   req.body,
    function(err, result){
    res.send(
        (err === null) ? {msg: ''} : {msg: err}
    );
   });
});

I get a 200 response back but my record is not updated. What is wrong with my syntax?

1
  • 2
    res.send will always send 200. Because it returns either '' or the error with status code 200 OK. If you have an error you should do res.status(500).send('Something broke!'); Commented Feb 6, 2015 at 22:49

2 Answers 2

5

You need to make sure you're turning the string _id into an ObjectId.

Also, you were using res.body instead of req.body.

router.put('/updateuser/:id', function(req, res) {
    var db = req.db;
    var userToUpdate = req.params.id;
    db.collection('userlist').update({ _id: ObjectId(userToUpdate)}, req.body, function (err, result) {
        res.send(
            (err === null) ? {msg: ''} : {msg: err}
        );
    });
});

Different drivers use a different method to create an ObjectId:

  • mongoDB native driver: new ObjectId(idString);
  • mongoJS: db.ObjectId(idString);
  • mongoSkin: toObjectID(idString);
  • mongoose: mongoose.Types.ObjectId(idString);
Sign up to request clarification or add additional context in comments.

9 Comments

I implemented those changes and now get a 500 error. It has something to do with the db.ObjectId() method but I do not know what it is.
I'm assuming that db is your mongoDB driver, so it should work. db.ObjectId() might not be a method of created an object id for your database driver. It is for mongojs, but if you are using another mongo driver, db.ObjectId() might not work. Also, what is the error stack received?
you can try new ObjectID(idString); this is the method the native mongoDB uses. Mongoose you use mongoose.Types.ObjectId(string);
My db is the "mongo.db(url)" database connection. The error stack from the node app doesn't say much. PUT /users/updateuser/54d3ba6cfe49c07c04733fac 500 42.412 ms
I am using mongoskin fyi
|
1

It should be req.body, not res.body

db.collection('userlist').update(
{ _id: userToUpdate},
   res.body -> should be req.body

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.