[SOLVED] Combining group and match in query

Hi,

I want to combine group by and match, but seems doing something wrong somewhere. Can someone please guide here:

    const pipeline = [
        { 
          match: {trancheId: 2},
          group: { objectId: null, total: { "$sum": { "$toInt" : "$amount"} } } }
      ];

@cryptokid any help here please?

by looking on other posts, like Moralis lookup response
it looks like the syntax needs more {}, like

const pipeline = [
         {  match: {trancheId: 2}},
         { group: { objectId: null, total: { "$sum": { "$toInt" : "$amount"} } } }}
      ];
1 Like

Even this one is also not working:

  const pipeline = [
      {  match: {trancheId: 2 } },
      { group: { objectId: null, total: { "$sum": { "$toInt" : "$amount"} } } }
    ];

The error says:

Cannot read properties of undefined (reading β€˜total’)

But if I remove the match block, it works perfectly fine:

  const pipeline = [
      { group: { objectId: null, total: { "$sum": { "$toInt" : "$amount"} } } }
    ];

and if you run only this it also runs perfectly?

your solution actually worked!
trancheId is string, hence needed to pass as string value.

Thanks again man for your prompt response!

1 Like