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];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.