Rather than duplicate the code as shown, just set parameters for the find() methods and then call the same function. Here is the function. I didnt, but you could, add parameters. ( cl() is my version of console.log(). const cl = (…args) => console.log(…args)).
async function getCourses() {
const result = await Course.find(query).or(qor).sort(qsort).select(qselect);
cl(result);
}
And here is the data for each exercise according to how the function expected the data:
//14. Exercise 1
let query = { isPublished: true, tags: "backend" };
let qsort = { name: 1 };
let qor = [{}]; // default if you dont need an OR
let qselect = "name author";
getCourses();
// // 15. Exercise 2
query = { isPublished: true, tags: { $in: ["backend", "frontend"] } };
qsort = { price: -1 };
qor = [{}];
qselect = "name author price";
getCourses();
// //16. Exercise 3 - >=$15, published, OR "by" in title
query = { isPublished: true };
qor = [{ price: { $gte: 15 } }, { name: /.*by.*/i }];
qsort = { price: 1 };
qselect = "name author price";
getCourses();