【发布时间】:2023-03-10 15:12:01
【问题描述】:
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");
});
但是如何更新嵌入的子部分期间不起作用->使用猫鼬排名
【问题讨论】:
-
description是您查询中的唯一字段吗?如果您可以将句点数组作为查询文档的一部分,即WorkItem.update({ "description": req.body.description, "period.rank": { "$ne": 3 } },{ "$set": { "period.$.rank": 3 } }, callback);,则可以在更新中使用positional operator$