I have a Chapter schema like this:
const ChapterSchema = new Schema({
title: {
type: String,
required: [true, 'Chapter title is required']
},
topics: { type: [TopicSchema] }
})
So, there is a topics array as sub-documents of Chapter.
I want to get a particular topic by its _id from Chapter. For that I've tried this query below:
let data = await Chapter.findOne({ "topics._id": _id })
return res.json(data)
But it returns a whole chapter of this topic with topic sibling like this:
{
"_id": "5e504271ee36f61ba8d76f37",
"title": "Roshayon Chapter 2",
"topics": [
{
"_id": "5e52bdf994b60b4c540cab33",
"title": "topic 4",
"intro": "<p><b>This text is bold</b></p><p><i>This text is italic</i></p><p>This is<sub> subscript</sub> and <sup>superscript</sup></p>"
},
{
"_id": "5e52bdf994b60b4c540cab34",
"title": "topic 5",
"intro": "<p><b>This text is bold</b></p><p><i>This text is italic</i></p><p>This is<sub> subscript</sub> and <sup>superscript</sup></p>"
}
]
}
I don't need whole chapter as above. I just need a single topic object which I am looking for by its id. How can I able to get
Expected result:
{
"_id": "5e52bdf994b60b4c540cab34",
"title": "topic 5",
"intro": "<p><b>This text is bold</b></p><p><i>This text is italic</i></p><p>This is<sub> subscript</sub> and <sup>superscript</sup></p>"
}