Reputation: 1364
id: { type: String, required: true, unique: true, default: uuid.v1 },
description: { type: String },
period: [{
id: { type: String, default: uuid.v1 },
start: { type: Date, default: Date.now },
due: { type: Date },
dueWarnByHours: { type: Number, integer: true },
newnessByHours: { type: Number, integer: true },
}],
i have an embedded mongodb database document like this. i tried updating it like the below one
WorkItem.update({ description: req.body.description},{period.rank: 3}, function(err, req) {
if (err) return console.error(err);
console.dir(reqWorkItemId + "Successfully removed the workItem from the database");
});
but it isn't working how to update the embedded child part period->rank using mongoose
Upvotes: 2
Views: 1022
Reputation: 2776
The following code will help you:-
var findQuery = { description: req.body.description, 'period.id' : someId};
WorkItem.update(findQuery,{$set:{'period.$.rank': 3}}, function(err, req) {
if (err) return console.error(err);
console.dir(reqWorkItemId + "Successfully removed the workItem from the database");
});
OR
WorkItem.update(findQuery,{$set:{'period.$.rank': 3}}, function(err, req) {
if (err) return console.error(err);
console.dir(reqWorkItemId + "Successfully removed the workItem from the database");
});
NOTE:- This will only update the first object of period array.
Upvotes: 2
Reputation: 1364
WorkItem.update({ id: d }, { description: req.body.description, $set: { 'status.0.rank': req.body.status.rank } },
function(err, numRowsAffected, raw) {
if (err) return console.error(err);
if (numRowsAffected > 0) {
console.dir("reqWorkItemId" + "Successfully removed the workItem from the database");
} else {
console.log("fail");
//res.send(500, { error: 'carrier not updated' });
}
});
This one works for me
Upvotes: 2