Mongoose : Update not working

The basic idea is something like this:

model.find({}, function(err, docs) {
    docs.forEach(function(doc) {
        // update it with whatever new object
        model.update({"category":"cat1"});
    });
});

Now my question is any sort of update/save doesn't work. I can find, count, etc with the model. I've tried doc.save(), model.findOneAndUpdate(), using the $set parameter for particular fields, they don't work. I went to mongo shell and typed the update command with the parameter, it worked.

One time it did work, was when I tried to put new documents using var m = new model({..}) and m.save() from outside the callback function of find(). But I need to find documents by their fields, and update those.

I feel like I'm missing something really elementary, could someone help me out?

edit: mongoose.disconnect() was being called later on in the code. So query did not execute.

Another possibility cause to this problem is when you use Mixed types, as in Schema.Types.Mixed. If you only update the Mixed field, then Mongoose doesn't know you've updated that field. So you need to call markModified

I ran into this recently and it was a pain in the neck to find out what was going on.

Unless it's just a typo, you're doing an update on the wrong object. You probably want to do

model.find({}, function(err, docs) { docs.forEach(function(doc) { doc.update({/fields here/}); }); });

Above answer is incorrect.

Edit

After thinking a little about it, why are you doing find at all? You should be able to update your docoments without it. Check the API docs for update

model.update({}, {field1: 'updated value'}, function(err) {});