MongoDB: how to parse date in 3.6 mongoDb version?

Use $dateToParts and its counterpart $dateFromParts

Here is an updated Playground

What it does is basically break the date into its parts:

{
  $project: {
    dateHour: {
      $dateToParts: {
        date: "$updatedAt"
      }
    }
  }
}

would produce:

{
  "dateHour": {
    "day": 19,
    "hour": 18,
    "millisecond": 0,
    "minute": 21,
    "month": 3,
    "second": 5,
    "year": 2020
  }
}

and then later you reconstruct the date from its parts:

{
  $project: {
    reconstructedDateHour: {
      $dateFromParts: {
        year: "$dateHour.year",
        month: "$dateHour.month",
        day: "$dateHour.day",
        hour: "$dateHour.hour"
      }
    }
  }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)