Mongodb Aggregation Framework | double match

aggregation-framework, mongodb

Solution

Each of the operators in your `aggregate` pipeline need to be separate objects. Also, while some versions of the shell (and drivers) may allow passing the objects as separate parameters, the correct way is to pass them wrapped in a single array. Try this instead:

db.log.aggregate([
  { $match: { v: 1, t: "trainingStep" } },
  { $group: { _id: {userId: '$u', questionId: '$s'}, counts: {$sum: 1} } },
  { $match: { 'counts': {$gte: 2} } }
])

Problem

Here's what it looks like - ``` db.log.aggregate({ $match:{ v:1, t:"trainingStep" }, $group:{ _id:{userId:'$u',questionId:'$s'}, counts:{$sum:1} }, $match:{ 'counts':{$gte:2} } }) ``` - The first match works perfect. - The group works perfect and pushes out exactly what I'm looking for. - The last $match does not work and is showing all counts instead of my requested >=2 I've tried 'counts', '$counts', "$counts"... but none did the trick!

Original source