Hi folks,
I am trying to update a deeply nested array document but get an error that the path could not be found in the schema.
So I am trying to push a new Chapter to the chapters array.
This is my code:
const result = await Store.updateOne(
{
_id: new ObjectId(storeId),
"products._id": ObjectId.createFromHexString(productId),
"products.sections._id": ObjectId.createFromHexString(sectionId),
},
{
$push: {
"products.$.sections.$[sect].chapters": newChapter,
},
},
{
arrayFilters: [{ "sect._id": ObjectId.createFromHexString(sectionId) }],
}
);
And this is my CourseModel:
import mongoose from "mongoose";
const chapterSchema = new mongoose.Schema({
title: { type: String, required: true },
content: { type: String, required: true },
videoUrl: { type: String },
attachments: [{ type: String }],
position: { type: Number, required: true },
});
const sectionSchema = new mongoose.Schema({
title: { type: String, required: true },
description: { type: String, required: true },
chapters: [chapterSchema],
position: { type: Number, required: true },
});
const courseSchema = new mongoose.Schema({
userId: { type: String, required: true },
productType: { type: String, required: true },
title: { type: String, required: true },
description: { type: String, required: true },
price: { type: Number },
imageUrl: { type: String },
icon: { type: String },
backgroundColor: { type: String },
textColor: { type: String },
position: { type: Number },
sections: [sectionSchema],
createdAt: { type: Date, default: Date.now },
});
const Course = mongoose.models.Course || mongoose.model("Course", courseSchema);
export default Course;
I double checked that the section and productId are correct, I also made sure the types are correct. Really stuck and hope somebody can help thanks