Aggregation with indexed date field not working

Completing my previous post : it seems that using $$NOW is the reason why index on creation_date is not used.

Using $$NOW :

  {
    $match: {
      $expr: {
        $gte: [
          "$creation_date", {
            $subtract: ["$$NOW", 345600000]
          }
        ]
      }
    }
  }

Without $$NOW :

  {
    $match: {
      $expr: {
        $gte: [
          "$creation_date", {
            $subtract: [ISODate("2024-05-29T17:11:00.000+00:00"),345600000]
          }
        ]
      }
    }
  }

1 Like