Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
how can i add filter for some fields as optional after doing $aggrigate $lookup? what's changes are required?

i want so fetch data by subject and by grade and also some other fields........................................................................................................................................................................................................................................

 
   const skip = Number(page) === 1 ? 0 : Number(perPage) * (Number(page) - 1);
   const limit = Number(skip) + Number(perPage);
 
   let queryData: any = {};`enter code here`
   
   if (grade != null) {
     queryData.grade = grade;
   }
   
   if (subjects != null) {
     queryData.subjects = subjects;
   }
  let teacherData= await this.teacherModel.aggregate([

   {
     $lookup: {
       from: "eclasses",
       localField: "_id",
       foreignField: "teacher",
       as: "classes"
     }
   },
   
   {
     $lookup: {
       from: "courses",
       localField: "_id",
       foreignField: "teachers.id",
       as:"course",
     }
     
   },
   {
     $project:{firstName:1, lastName:1,"classes.grade":1,"classes.subject":1,"classes.name":1,"course._id":1}
   } ,
   {       
     $match: {...queryData}
   },
   {
     $facet: {
       TeacherRecord: [{ $skip: skip }, { $limit: limit }],
       metadata: [{ $count: "total" }, { $addFields: { page: page } }],
     },
   },
   
  
 ])
 const totalCount = teacherData[0]?.total
 const getPaginationResult = PaginationHandle.getPageCount(
   page,
   perPage,
   totalCount
 );

 return [ teacherData,getPaginationResult];

 }```
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!