Question: ******************************** Mongo DB ********************** Populate a courses collection using the following command (make sure that no documents exist in the collection before running the command).
******************************** Mongo DB **********************
Populate a courses collection using the following command (make sure that no documents exist in the collection before running the command).
db.courses.insertMany( [
{
title: "Introduction to Programming",
course_id: "CS-101",
dept: "Computer Science",
sections: [ { sec_id: "001", instructor: "Zhang", year: 2017 },
{ sec_id: "002", instructor: "Levy", year: 2017 },
{ sec_id: "001", instructor: "Zhang", year: 2018 } ]
},
{
title: "Data Structures",
course_id: "CS-201",
dept: "Computer Science",
sections: [ { sec_id: "001", instructor: "Zhang", year: 2017 },
{ sec_id: "001", instructor: "John", year: 2018 },
{ sec_id: "002", instructor: "John", year: 2018 } ]
},
{
title: "Database",
course_id: "CS-310",
dept: "Computer Science",
sections: [ { sec_id: "001", instructor: "Levy", year: 2017 },
{ sec_id: "001", instructor: "John", year: 2018 } ]
},
{
title: "Business Fundamentals",
course_id: "BUS-101",
dept: "Business",
sections: [ { sec_id: "001", instructor: "Brandt", year: 2017 },
{ sec_id: "001", instructor: "Davis", year: 2018 } ]
},
{
title: "Business Statistics",
course_id: "BUS-210",
dept: "Business",
sections: [ { sec_id: "001", instructor: "Davis", year: 2017 },
{ sec_id: "001", instructor: "Lee", year: 2018 } ]
},
{
title: "Investments",
course_id: "BUS-305",
dept: "Business",
sections: [ { sec_id: "001", instructor: "Brandt", year: 2018 },
{ sec_id: "002", instructor: "Lee", year: 2018 } ]
}
] );
Question
1.
- Find the number of instructors who taught in each year and sort the result in the chronological order. The result is given below for your reference (the field name and order must be the same). You may check the $addToSet operator.

2. Find the number of sections for each course in each year and sort the result first in the alphabetical order of the course title and then in the chronological order. The result is given below for your reference (the field name and order must be the same). You may use $project to format your result.

\( \left[\begin{array}{l}\{\text { year: 2017, num_instructors: } 4\} \text {, } \\ \{\text { year: 2018, num_instructors: } 5\}\end{array} ight. \) \{title: 'Business Fundamentals', year: 2017, num_sections: 1}, \{title: 'Business Fundamentals', year: 2018, num_sections: 1}, { title: 'Business Statistics', year: 2017, num_sections: 1}, { title: 'Business Statistics', year: 2018, num_sections: 1}, { title: 'Data Structures', year: 2017, num_sections: 1}, { title: 'Data Structures', year: 2018, num_sections: 2}, { title: 'Database', year: 2017, num_sections: 1}, { title: 'Database', year: 2018, num_sections: 1}, \{title: 'Introduction to Programming', year: 2017, num_sections: 2} { title: 'Introduction to Programming', year: 2018, num_sections: 1} { title: 'Investments', year: 2018, num_sections: 2}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
