I can't find a way to edit nested data more than on 2nd level. I have next model structure:
var Category = new Schema({
name: String,
url: String,
id: String
});
var Subgroup = new Schema({
name: String,
url: String,
id: String,
categories: [Category]
});
var Group = new Schema({
name: String,
url: { type: String, default: '' },
subgroups: [Subgroup]
});
module.exports = mongoose.model('group', Group);
I need to edit certain Category through the Group model. I tried all possible variations of working with nested arrays but have no result.
Group.findOneAndUpdate({
_id: req.params.groupId,
"subgroups.categories._id": req.params.categoryId
}, {
$set: {
"subgroups.categories.$.name": attributes.name
}
});