Reputation: 2733
I'm finding documents by _id
in a loop and updating a boolean in each document:
db.items.findById(key, function(error, item) {
item.flags.cake = false;
item.update(function(error, zzz) {
if(error) return next(error);
console.log('output ',zzz);
});
});
But the documents will not update. The mongoose schema for item
:
flags: {
cake:Boolean
}
Upvotes: 0
Views: 44
Reputation: 103305
Use the save()
method instead which makes use of a callback that will receive three parameters you can use:
1) err
if an error occurred
2) item
which is the saved item
3) numAffected
will be 1 when the document was successfully persisted to MongoDB, otherwise 0.
Items.findById(key, function(error, item) {
item.flags.cake = false;
item.save(function (err, item, numAffected) {
if (err) console.log(err)
console.log('output ', item);
});
});
As an extra measure of flow control, save will return a Promise.
item.save().then(function(item) {
console.log('output ', item);
});
Upvotes: 1