Skip to content

Instantly share code, notes, and snippets.

@sahilkashyap64
Created March 21, 2020 19:35
Show Gist options
  • Save sahilkashyap64/64fbf084509f3121b48435577d68f37c to your computer and use it in GitHub Desktop.
Save sahilkashyap64/64fbf084509f3121b48435577d68f37c to your computer and use it in GitHub Desktop.
Find the number of object present in array of mongoDB
const Allstudentsaag = await Attendance.aggregate(
[ { $match : { "subject" : "chem" } } ,
{ $project : { _id: 0,email:1, professorclass : 1 ,
numberofstudent: { $cond: { if: { $isArray: "$professorclass" },
then: { $size: "$professorclass" }, else: "NA"} }}},]);
//In attendance collection Find the subject=chem ,only show email and professorclass field
if professorclass is an array then show the size of the array if it's empty so N/A.
Show the size in a fieldname called numberofstudent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment