Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb Aggregation Framework | double match

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} }
})
  1. The first match works perfect.
  2. The group works perfect and pushes out exactly what I'm looking for.
  3. 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!

like image 510
MKN Web Solutions Avatar asked Oct 30 '12 18:10

MKN Web Solutions


1 Answers

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} } }
])
like image 153
JohnnyHK Avatar answered Oct 13 '22 06:10

JohnnyHK