const likedInterns = company.likedInterns;
const liked = url.searchParams.get("liked");
const interns = await db.models.intern
.find(
search
? {
name: { $regex: new RegExp(search, "i") },
}
: {}
/* Show only liked interns - Not working */,
liked
? {
_id: { $elemMatch: likedInterns },
}
: {}
)
.sort({ updatedAt: -1 });
return [interns, tags, userId, companyId];
}
Try to build your filter
before the query and use the $in operator
const likedInterns = company.likedInterns;
const liked = url.searchParams.get('liked');
let filter = {};
if (search) filter = { ...filter, name: { $regex: new RegExp(search, 'i') } }
if (liked) filter = { ...filter, { _id: { $in: likedInterns } } }
const interns = await db.models.intern
.find(filter)
.sort({ updatedAt: -1 });
return [interns, tags, userId, companyId];