Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update embedded document mongoose

I'm looking for an easy way of updating an embedded document using mongoose without having to set each specific field manually. Looking at the accepted answer to this question, once you find the embedded document that you want to update you have to actually set each respective property and then save the parent. What I would prefer to do is pass in an update object and let MongoDB set the updates.

e.g. if I was updating a regular (non embedded) document I would do this:

models.User.findOneAndUpdate({_id: req.params.userId}, req.body.user, function(err, user) {
    err ? resp.status(500).send(err) : user ? resp.send(user) : resp.status(404).send();
});

Here I don't actually have to go through each property in req.body.user and set the changes. I can't find a way of doing this kind of thing with sub documents as well ?

My Schema is as follows:

var UserSchema = BaseUserSchema.extend({ 
    isActivated: { type: Boolean, required: true },
    files: [FileSchema]
});

var FileSchema = new mongoose.Schema(
    name: { type: String, required: true },
    size: { type: Number, required: true },
    type: { type: String, required: true },
});

And I'm trying to update a file based on user and file id.

Do I need to create a helper function to set the values, or is there a MongoDB way of doing this ?

Many thanks.

like image 417
Sherlock Avatar asked Dec 20 '25 12:12

Sherlock


1 Answers

Well presuming that you have something that has you "filedata" in a variable, and of course the user _id that you are updating, then you wan't the $set operator:

var user = { /* The user information, at least the _id */
var filedata = { /* From somewhere with _id, name, size, type */ };

models.User.findOneAndUpdate(
    { "_id": user._id, "files._id": filedata._id },
    {
        "$set": {
            "name": filedata.name,
            "size": filedata.size,
            "type": filedata.type
        }
    },
    function(err,user) {
      // Whatever in here such a message, but the update is already done.
    }
);

Or really, just only $set the fields that you actually mean to "update" as long as you know which ones you mean. So if you only need to change the "size" then just set that for example.

like image 129
Neil Lunn Avatar answered Dec 23 '25 04:12

Neil Lunn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!