How can i build mongoose model from this JSON
Your comment schema is a bit confusing. You created a "Comment" schema (note this is singular) yet you are trying to use the comment schema as an array of comments.
Tip: Keep your schemas and singular.
To achieve your desired design whilst keeping your models singular you can try this:
Solution (1): Have a single "Section" collection storing all the dataNote: although you have multiple schemas, we only ever model the SectionSchema and it will be our only collection where every Section document contains all the info you need.
const sectionSchema = new Schema({ name: { type: String, required: [true, 'Section name is required'] }, comments: { type: [commentSchema] }});const replySchema = new Schema({ //id: String, //not needed, mongoose will automatically add an "_id" property when you save authorAvatarUrl: String, authorName: String, authorId: String, authorUrl: String, comment: String, parentId: String});const commentSchema = new Schema({ //id: String, //not needed, mongoose will automatically add an "_id" property when you save authorAvatarUrl: String, authorName: String, authorId: String, authorUrl: String, comment: String, replies:{ type: [replySchema] }});module.exports = mongoose.model("Section", sectionSchema);
In the above solution, you can still have routes to address only specific comments, for example:
router.get("/sections/:id") //gets the entire section document matching the IDrouter.post("/sections/:id/comments/") //creates a new comment within section with matching ID. Simply push a new comment into the comments arrayrouter.get("/sections/:id/comments/") //gets all the comments for this section. Simply return only the comments property of the section document.router.get("/sections/:id/comments/:commentId") //gets a specific comment within a specific section. Filter the comments array to get the comment matching the commentId and then only return that.etc..
Final note: there are other ways you can model your data. This is just one example. For instance, you can have a section collection and a comment collection, where a comment document stores a section Id indicating the section that that comment document relates to.
Have a look at https://mongoosejs.com/docs/subdocs.html and https://docs.mongodb.com/manual/core/data-modeling-introduction/ it might help you to understand the different ways you can model your data and also how mongoose subdocuments work and how to use them.