I want to get data between two date mongoose

I’m working with Nestjs graphql

let logs = await this.profileModel.aggregate([
        {
          $match: {
            bindedBanque: 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',
          },
        },
        { $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: '$logsByTpes.outcome' },
            count: { $sum: 1 },
          },
        },
      ]);
      console.log(logs);

      return logs;

I checked the data type of the field was Date and the input was Date

By the way it worked with the function below

  async getLogsByDate(startDate, endDate) {
    let data = await this.logModel.aggregate([
      { $match: { transactionDate: { $gte: startDate, $lt: endDate } } },
      {
        $group: {
          _id: { _id: '$outcome' },
          count: { $sum: 1 },
        },
      },
    ]);

    const computedValue = data.map((data) => {
      return { name: data._id._id, value: data.count };
    });
    console.log('computedValue', computedValue);

    return computedValue;
  }

I really stuck any one could help me

Hi @skander_lassoued,
A similar issue was fixed by wrapping the input date into a JavaScript Date Object like this:

But since you mentioned the following already:

In order to better analyze your situation can you provide some sample documents from the profile collection?
Also, can you please provide the startDate and endDate values with their actual formats and values by console logging their types using console.log(typeof startDate) & console.log(typeof endDate)?

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

1 Like

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