I am trying to filter all objects in the projects array that are not in the savedProjects array but the result is an empty array. Am I filtering the wrong way?
My code:
router.post('/get-related', async (req, res) => {
console.log(req.body);
try {
const projects = await Projects.find({researchers: req.body.fullname});
let savedProjects = await Projects.find({projectID: req.body.projectsIDs});
projects.filter(p => savedProjects.includes(p) === false);
res.json(projects);
} catch (err) {
console.log(err);
res.json({ message: err }) };
});
Using the $nin specification fixed it. I imported the saved projects and then queried all projects that did not include the ones in the savvedProjects array.
router.post('/get-related', async (req, res) => {
try {
const savedProjects = await Projects.find({projectID: req.body.projectsIDs});
let IDs = savedProjects.map(p => p.projectID);
const projects = await Projects.find({ category: req.body.researcharea, projectID: { $nin: IDs }});
res.json(projects);
} catch (err) { res.json({ message: err }) };
});