How to get size of the syllabus array

There is an issue with the query.

Consider the following documents:

{ _id: 0, syllabus: 369 }
{ _id: 1, syllabus: [ 369 ] }
{ _id: 2 }

The query will work fine with _id:1 and _id:2 but will fail with _id:0. This case seems to be absent from your data since

The following variation will work in all the cases.

db.subject_staff_database.aggregate([{$project: {
numberOfCourses :{$cond: [{$eq: [{$type:"$syllabus"}, "array"]}, {$size :"$syllabus"}, 0 ]}
}}])

The difference being

{$eq: [{$type:"$syllabus"}, "array"]}

vs

{$ne: [{$type:"$syllabus"}, "missing"]}

@Sudhesh_Gnanasekaran, please read Formatting code and log snippets in posts before posting your next code snippet. This will ensure we can cut-n-paste your query and use it directly without editing the fancy html quotes we get when it is not marked up correctly.