Home > other >  Javascript aggregate with multiple matches
Javascript aggregate with multiple matches

Time:03-23

I am trying to create a aggregate pipeline with multiple matches, but not sure how to go about it?!?

Here is what I am trying:

const pipeline = [
  { match: { oomId: oomId, holes: 0, collectPutts: true } },
  { group: { 
    objectId: '$userId',
    puttsPlayed: { $sum: 1 },
    puts: {$sum: '$puts'},
    gir: {$sum: '$gir'},
    girHcp: {$sum: '$girHcp'}
  } },
  { match: { oomId: oomId, holes: 0, collectHits: true } },
  { group: { 
    objectId: '$userId',
    hits: {$sum: '$hit'},
    holesHit: {$sum: '$holesToHit'}
  } },
  { match: { oomId: oomId, holes: 0 },
  { group: { 
    objectId: '$userId',
    totalPlayed: { $sum: 1 },
    points: {$sum: '$points'},
    strokes: {$sum: '$strokes'}
  } },
  { project:{ 
      played: '$totalPlayed',
      puttsPlayed: '$puttsPlayed',
      puts: {$divide: ['$puts', '$puttsPlayed']},
      gir: {$divide: ['$gir', '$puttsPlayed']},
      girHcp: {$divide: ['$girHcp', '$puttsPlayed']},
      hits: '$hits',
      holesHit: '$holesHit',
      hitsPercentage: {
        $multiply: [
          { $divide: [ "$hits", "$holesHit" ] },
          100
        ]
      },
      points: {$divide: ['$points', '$totalPlayed']},
      strokes: {$divide: ['$strokes', '$totalPlayed']}
  }}
];

I am getting this error: Unexpected token '{'

Not sure where the error is, and is it even possible to do what I am trying to do? or should I user 3 different pipelines?

Any help is appreciated... New to aggregate :-/

CodePudding user response:

You didn't close the brackets after 3rd $match stage. So, instead of this:

{ match: { oomId: oomId, holes: 0 },

Do this:

{ match: { oomId: oomId, holes: 0 } },
  • Related