Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
The syntax for the $hour
operator is as follows:
{
$hour: <dateExpression>
}
Description | |
---|---|
dateExpression |
An expression that resolves to a Date, Timestamp, or ObjectId. If the expression resolves to null or is missing, $hour returns null. |
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
}
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB