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 },
}],
我有一个像这样的嵌入式mongodb数据库文档。我尝试像下面那样更新它
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");
});
但如何使用 Mongoose 更新嵌入的子部分 period->rank 不起作用
最佳答案
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' });
}
});
这个对我有用
关于node.js - Mongoose 更新嵌入文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39367658/