0

I want to filtre data using $match and group it using $group
since I use dynamic variable in $match stage I got no result,

 let logs = await Promise.all(
      tpes.map(async (item) => {
        return await this.logModel.aggregate([
          { $match: { terminalId: item } },
          {
            $group: {
              _id: '$outcome',
              value: {
                $sum: 1,
              },
            },
          },
          {
            $project: {
              name: '$_id',
              value: 1,
              _id: 0,
            },
          },
        ]);
      }),
    );
    console.log('logs', logs);

logs here is an array contain ids from where I want to get data

Fredrick
  • 37
  • 3
  • Does this answer your question? [JavaScript set object key by variable](https://stackoverflow.com/questions/11508463/javascript-set-object-key-by-variable) – Tobias S. Aug 06 '22 at 21:48
  • mongoose not js pure – Fredrick Aug 06 '22 at 21:57
  • You can use it, i'm assuming you just need to cast it from `string` to `ObjectId`. – Tom Slabbaert Aug 07 '22 at 06:58
  • Check this answer. As they say above, you need to cast to ObjectId. Either using mongoose or native driver. https://stackoverflow.com/questions/7825700/convert-string-to-objectid-in-mongodb – nacho Sep 07 '22 at 15:27

0 Answers0