I want to write a method in node js with mongodb that filters users by
- both batch and course 2)only batch if course not enterted 3)only course if batch not enterted
//filter students by either course or batch
export const getUsersByCourseOrBatch = async (req, res) => {
try {
const { course, batch } = req.body
if(course, batch)
{
const users = await UserModel.find({ course: course, batch: batch })
}
else if(course)
{
const users = await UserModel.find({ course: course })
}
else
{
const users = await UserModel.find({ batch: batch })
}
res.status(200).json({
message: `Users from course ${course} and batch ${batch}`,
users,
})
} catch (error) {
res.status(500).json({ message: error.message })
}
}