How to get the subcategory object with find function in monoose?
This is how Category model look like:
const Category = mongoose.model(
'Category',
new mongoose.Schema({
name: { type: String },
subCategories: [
{
name: { type: String }
}
]
})
);
I need to find only one of subCategories
with id of 5ccdc3dd4e88235af8923c74.
So I did the query:
Category.find({ "subCategories._id": { '$in': [ mongoose.Types.ObjectId("5ccdc3dd4e88235af8923c74") ] } })
The results comes with the right Category But also with all subCategories that belong to this category and I need only matched subcategory.
How I improve this query to get me the right subCategory and the category nested inside? I only want to use find method for this (no lookup, no arrgigate).