found a lot of questions like these here, but not an answer.
Lets say I have the following mongoose schema:
const mySchema = new mongoose.Schema({
sanePeoplesField: String,
comments: [
normalStuff: {type: Date, default: Date.now},
damNestedAgain: [String]
]
})
So to recap, damNested array
is inside the comments array
on the schema.
If I was lucky and wanted to change normalStuff (obj inside an array), I'd do this:
mySchema.findOneAndUpdate({"comments._id": req.body.commentId},
{
$push:
{
comments: { normalStuff: 12122020 } }
}
})
This would've updated normalStuff with a new value.
However, I need to update a field in damNestedAgain
, but don't know how to reach it!
How to update the nested array of a nested array, damNestedAgain
, in my example?
mySchema.findOneAndUpdate({"comments._id": req.body.commentId},
{
$push:
{
"comments.$.damNestedAgain": req.body.commentId
}
})
That has done the trick, thanks.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.