How can I match all data between two date

I want to get data between two dates if I give date 01-01-2022 and date 09-10-2022 I want to display all data between these two dates I did this example bellow:

let logs = await this.profileModel.aggregate([
    {
      // finish here date
      // finish settlement
      // finish logReport
      $match: {
        bindedSuperAdmin: name,
        // transactionDate: { $gte: startDate, $lt: endDate },
      },
    },
    {
      $lookup: {
        from: 'tpes',
        localField: 'nameUser',
        foreignField: 'merchantName',
        as: 'tpesBySite',
      },
    },
    {
      $lookup: {
        from: 'logs',
        localField: 'tpesBySite.terminalId',
        foreignField: 'terminalId',
        // as: 'logsByTpes',
        pipeline: [
          {
            $match: {
              
              transactionDate: { $gte: startDate, $lte: endDate },
              // transactionDate: { $in: [startDate, endDate] },
            },
          },
        ],
        as: 'logsByTpes',
      },
    },

    { $unwind: '$tpesBySite' },

    { $unwind: '$logsByTpes' },
    {
      $project: {
        // bindedSuperAdmin: '$bindedSuperAdmin',
        // bindedBanque: '$bindedBanque',
        // bindedClient: '$bindedClient',

        snTpe: '$tpesBySite.sn',
        terminalId: '$tpesBySite.terminalId',

        transactionDate: '$logsByTpes.transactionDate',
        transactionTime: '$logsByTpes.transactionTime',

        outcome: '$logsByTpes.outcome',
      },
    },
    {
      $group: {
        _id: { bank: '$outcome' },
        count: { $sum: 1 },
      },
    },
  ]);

  return logs;

It matches only the given dates but I need to gt all data between these two dates I really get stuck how can I Fix It Please

It should work.

Most likely some of your dates are not Date, otherwise you would NOT have specified your dates as

You cannot do $gt and $lt on dates are with this format because it makes the dates unordered.

Share some input documents.

This is probably more simple than requested but it will show you how you can find between two dates (example of login dates):

db.users.find({"lastLogin":{$gte: ISODate("2022-08-01T00:00:00.000Z"), $lte: ISODate("2022-08-31T00:00:00.000Z")}})

I do not understand how a simple find() can be the solution to an aggregation that has 2 $lookup stages, some $unwind, a $project and the $group.

How to make a lookup with just simple find() ? :thinking:

I think that was the point @steevej was trying to make, you can’t. You need to use the aggregation pipeline. The example I posted was an example of finding something between two dates but your aggregation was much more complex with other stages.

You can take the

{
  "lastLogin":{
    $gte: ISODate("2022-08-01T00:00:00.000Z"),
    $lte: ISODate("2022-08-31T00:00:00.000Z")
  }
}

of the find() query and put that in a $match stage in your aggregate() command and then from there do your $lookup stages.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.