Cannot $match documents with a timestamp from the beginning of today

I am trying to retrieve documents from a timeseries collection in compass with the following aggregation stage:

  {
    $match: {
      timestamp: {
        $gte: {
          $dateTrunc: {
            date: new ISODate(),
            unit: "day",
            binSize: 1,
          },
        },
      },
    },
  },

However, this is returning no documents (I know some were created today). I do not want to hardcode a date into the query. I have tried using:

new Date().setHour(0,0,0,0)

I’m not sure if I can chain setHour() straight onto the new Date() - it doesn’t error but it doesn’t give me any results either. I can use it to add a field though, so it seems that compass is ignoring the function in some types of aggregations but not all.

I played with trying to create a variable within the stage but I cannot figure out the syntax for that.

I’m sure I’m missing something obvious but any help would be appreciated.

Hi @George_Richardson and welcome to the community.

If I understand the query correctly, you are trying to find the documents that match with the current date. Based on the above aggregation stage, I tried to insert some data data into the collection as:

Atlas atlas-xp4gev-shard-0 [primary] test> db.testMatch.find()
[
  {
    _id: ObjectId('65ddf76921382574857736dc'),
    timestamp: ISODate('1999-12-31T18:30:00.000Z'),
    data: 'Sample Data 1'
  },
  {
    _id: ObjectId('65ddf76921382574857736dd'),
    timestamp: ISODate('2024-02-27T10:15:00.000Z'),
    data: 'Sample Data 2'
  },
  {
    _id: ObjectId('65ddf76921382574857736de'),
    timestamp: ISODate('2024-02-27T12:30:00.000Z'),
    data: 'Sample Data 3'
  },
  {
    _id: ObjectId('65ddf76921382574857736df'),
    timestamp: ISODate('2024-02-26T15:45:00.000Z'),
    data: 'Sample Data 4'
  },
  {
    _id: ObjectId('65ddf76921382574857736e0'),
    timestamp: ISODate('2024-02-26T18:00:00.000Z'),
    data: 'Sample Data 5'
  },
  {
    _id: ObjectId('65ddf76921382574857736e1'),
    timestamp: ISODate('2024-02-26T20:30:00.000Z'),
    data: 'Sample Data 6'
  },
  {
    _id: ObjectId('65ddf76921382574857736e2'),
    timestamp: ISODate('2024-02-25T09:00:00.000Z'),
    data: 'Sample Data 7'
  },
  {
    _id: ObjectId('65ddf76921382574857736e3'),
    timestamp: ISODate('2024-02-25T11:20:00.000Z'),
    data: 'Sample Data 8'
  },
  {
    _id: ObjectId('65ddf76921382574857736e4'),
    timestamp: ISODate('2024-02-25T13:45:00.000Z'),
    data: 'Sample Data 9'
  },
  {
    _id: ObjectId('65ddf76921382574857736e5'),
    timestamp: ISODate('2024-02-24T14:00:00.000Z'),
    data: 'Sample Data 10'
  }
]

and I tried to use the match stage as:

[
  {
    $match: {
      timestamp: {
        $gte: new Date(
          new Date().setUTCHours(0, 0, 0, 0)
        ),
      },
    },
  },
]

which gives the output as:

[
  {
    _id: ObjectId('65ddf76921382574857736dd'),
    timestamp: ISODate('2024-02-27T10:15:00.000Z'),
    data: 'Sample Data 2'
  },
  {
    _id: ObjectId('65ddf76921382574857736de'),
    timestamp: ISODate('2024-02-27T12:30:00.000Z'),
    data: 'Sample Data 3'
  }
]

Let us know if the above query satisfies your requirement.

Best Regards
Aasawari

Thanks that’s great but I’m puzzled as to why you have to make a date object from a date object?

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