title: Mongodb aggregate query with not in
body: <p>I have a list of students in one collection and their grades in another collection. The schema (stripped of other details) look like </p>
  
  <p><strong>Students</strong></p>
  
  <pre><code>{
       _id: 1234,
      student: {
          code: "WUKD984KUK"
      }
  }
  </code></pre>
  
  <p><strong>Grades</strong></p>
  
  <pre><code>{
      _id: 3456,
      grade: 24,
      studentCode: "WUKD984KUK"
  }
  </code></pre>
  
  <p>There can be multiple grade entries for each student. I need a total count of students who are present in grades table but not in the student table. I also need a count of grades for each student who are not in the student table. The following is the query I had written, </p>
  
  <pre><code>var existingStudents = db.students.find({}, {_id: 0, 'student.code': 1});
  
  db.grades.aggregate(
      {$match: { 'studentCode': {'$nin': existingStudents}}},
      {$group: {_id: '$studentCode', count:{$sum: 1}}}, 
      {$project: {tmp: {code: '$_id', count: '$count'}}}, 
      {$group: {_id: null, total:{$sum:1}, data:{$addToSet: '$tmp'}}} 
  );
  </code></pre>
  
  <p>But this returns me all of the student details as if the match is not working. When I run just the first part of this query, I get the student details as </p>
  
  <pre><code>{ "student" : { "code" : "A210225504104" } }
  </code></pre>
  
  <p>I feel that because the return value is two deep the match isnt working. What is the right way to get this? </p>