Mongoose aggregation: Query within "group" phase Mongoose aggregation: Query within "group" phase mongoose mongoose

Mongoose aggregation: Query within "group" phase


You can use $lookup with pipeline,

  • $lookup to join Assignment collection
  • $match assignment id and is public true
  • $match outside lookup, check is assignment not empty [],
  { $match: ...  }, // skipping   // add this  {    $lookup: {      from: "Assignment",      let: { aId: "$assignment" },      pipeline: [        {          $match: {            $expr: { $eq: ["$_id", "$$aId"] },            isPublic: true          }        }      ],      as: "assignments"    }  },  // add this  { $match: { assignments: { $ne: [] } } },  { $group: ... }, // skipping  { $sort: ... } // skipping

Playground