[英]Mongoose update embedded document
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");
});
但是如何使用貓鼬更新嵌入式子部分期->排名是行不通的
以下代碼將幫助您:-
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");
});
要么
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");
});
注意:-這只會更新周期數組的第一個對象。
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' });
}
});
這個對我有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.