I have some document like this
db.instructors.insertMany( [
{
"name": "A",
"dept_name": "CS",
"salary": 68000,
"teaches": [ { course_id: "CS-101", year: 2018 }, { course_id: "CS-347", year: 2018 } ]
},
{
"name": "C",
"dept_name": "Finance",
"salary": 90000,
"teaches": [ { course_id: "FIN-201", year: 2016 }, { course_id: "FIN-301", year: 2017 }, { course_id: "FIN-320",
year: 2017 } ]
},
{
"name": "B",
"dept_name": "Biology",
"salary": 72000,
"teaches": [ { course_id: "BIO-101", year: 2016 }, { course_id: "BIO-201", year: 2017 }, { course_id: "BIO-101",
year: 2018 } ]
}
] );
I have to find the name, department name, and the salary of each instructor who has salary higher than 75,000 and has taught 2 courses. If an instructor has taught the same course N times, it is counted as N courses. I figured out that I have to use aggregate, but I cannot display the department name and salary of each instructor. Here my code:
db.instructors.aggregate([{
   $match: {
     salary: {$gt: 75000}
   }},
   { $unwind: "$teaches" },
   {
     $group:
       {
         _id: "$name",
         totalCourses:
           {
             $sum: 1
           }
       }
   },
   {
     $match: {
       totalCourses: 2
     }
   }
 ]);
