Database

[{"note":"#","course":"216-088","title":"NEW STUDENT ORIENTATION","credits":"0","level":"U","restrictions":"; ; REQUIRED OF ALL NEW STUDENTS. PREREQ: NONE","section_listing":[{"section_note":null,"section":"Se 001","days":"W","hours":{"start":"1:30pm","end":null},"bldg_and_rm":{"bldg":"BUS","rm":"S230"},"instructor":"Gusavac","comments":"9 WKS BEGINNING WEDNESDAY, 9/6/00"},{"section_note":null,"section":"Se 002","days":"F","hours":{"start":"11:30am","end":null},"bldg_and_rm":{"bldg":"BUS","rm":"S171"},"instructor":"Gusavac","comments":"9 WKS BEGINNING FRIDAY, 9/8/00"}]},{"note":"#","course":"216-293","title":"BUSINESS ETHICS","credits":"3","level":"U","restrictions":"; ; PREREQ: NONE","section_listing":{"section_note":null,"section":"Se 001","days":"R","hours":{"start":"2:30pm","end":"5:10pm"},"bldg_and_rm":{"bldg":"BUS","rm":"S230"},"instructor":"Silberg"}},{"note":"#","course":"216-782","title":"OPERATIONS MODELING AND SIMULATION","credits":"2","level":"G","restrictions":"; ; PREREQ: GRAD ST; BUS ADM 780(P)","section_listing":{"section_note":"e","section":"Se 001","days":"T","hours":{"start":"5:30pm","end":"7:10pm"},"bldg_and_rm":{"bldg":"BUS","rm":"N110"},"instructor":"Liu"}},{"note":"#","course":"216-099","title":"RANDOM","credits":"0","level":"U","restrictions":"; ; REQUIRED OF ALL NEW STUDENTS. PREREQ: NONE","section_listing":[{"instructor":"Gusavac",},{"instructor":"Eric",}]},]

Query

db.collection.aggregate([{$set:{instructors:{$cond:{if:{$eq:[{$type:"$section_listing.instructor"},"array"]},then:{$setUnion:["$section_listing.instructor"]},else:"$section_listing.instructor"}}}},{$unwind:"$instructors"},{$group:{_id:"$instructors",count:{$count:{}}}},{$project:{_id:0,instructor:"$_id",count:1}}])

Result