$hour (date expression)

APPLIES TO: MongoDB vCore

The $hour operator returns the hour portion of a date as a number between 0 and 23. The operator accepts a date expression that resolves to a Date, Timestamp, or ObjectId.

Syntax

The syntax for the $hour operator is as follows:

{
  $hour: <dateExpression>
}

Parameters

Description
dateExpression An expression that resolves to a Date, Timestamp, or ObjectId. If the expression resolves to null or is missing, $hour returns null.

Example

Example 1: Extract hour from current date

This example demonstrates extracting the hour from the current date and time.

db.stores.aggregate([
  { $match: { "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" } },
  {
    $project: {
      name: 1,
      storeOpeningDate: 1,
      currentHour: { $hour: new Date() },
      documentHour: { $hour: "$storeOpeningDate" }
    }
  }
])

The query returns the current hour and the hour from the ObjectId creation time.

{
  "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
  "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
  "storeOpeningDate": ISODate("2024-09-23T13:45:01.480Z"),
  "currentHour": 10,
  "documentHour": 13
}