I have a object in mongodb. I want to filter their sections using status. If status is active
then only that has to be sent to the user.
Structure of the Course schema is:
{
_id: 'ObjectId',
name: 'NODE JS',
.
.
sections: [
{status: 'active', trainer: 'XYZ'}, {status: 'inactive', trainer: 'YZX'}, .....
]
}
below 'sections.status': 'active'
is working properly for filtering the courses but it is returning all the sections which are not active. How can I filter the array in mongoose query itself. Without handling the query results.
Course.findOne({ _id: req.params.id , 'sections.status': 'active' })
.exec((err, course) => {
if (err) {
logger.error(err);
res.status(500).json({ success: false, message: 'Error in getting data' });
} else if (!course) {
res.status(404).json({ success: false, message: 'No course details found' });
} else {
res.json({ success: true, course });
}
});