在这里找到了很多类似这样的问题,但没有答案。
假设我具有以下猫鼬模式:
const mySchema = new mongoose.Schema({
sanePeoplesField: String,
comments: [
normalStuff: {type: Date, default: Date.now},
damNestedAgain: [String]
]
})
所以回顾一下,damNested array
位于模式的comments array
内部。
如果我很幸运,并且想要更改normalStuff(数组内的obj),我会这样做:
mySchema.findOneAndUpdate({"comments._id": req.body.commentId},
{
$push:
{
comments: { normalStuff: 12122020 } }
}
})
这将使用新值更新normalStuff。
但是,我需要更新damNestedAgain
中的字段,但不知道如何到达它!
在我的示例中,如何更新嵌套数组damNestedAgain
的嵌套数组?
mySchema.findOneAndUpdate({"comments._id": req.body.commentId},
{
$push:
{
"comments.$.damNestedAgain": req.body.commentId
}
})
完成了技巧,谢谢。