Home > Enterprise >  ISO Date function working on date having 0 in time but not with proper time
ISO Date function working on date having 0 in time but not with proper time

Time:11-18

I am trying to search some data in collection so i add {dCreatedAt: ISODate('2022-11-16')} in filter so i have two data having following values in db

dCreatedAt: 2022-11-16T00:00:00.000 00:00,
dCreatedAt: 2022-11-15T13:31:18.513 00:00

Filter showing only first data not the second one.

CodePudding user response:

If you really want, you can use an aggregation pipeline:

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $eq: [
          {$dateTrunc: {date: "$dCreatedAt", unit: "day"}},
          {$dateFromString: {dateString: "2022-11-16"}}
        ]
      }
    }
  }
])

See how it works on the playground example

CodePudding user response:

Try to filter by range of dates (today and tomorrow):

{ dCreatedAt: { $gte: ISODate('2022-11-16'), $lt: ISODate('2022-11-17') } }
  • Related