$hour - Amazon DocumentDB

$hour

The $hour operator extracts the hour component from a date or timestamp field.

Parameters

  • dateExpression: The date to which the operator is applied. This must resolve to a valid BSON date (e.g., a field like $createdAt or a date literal).

A parameter can also be specified as a document in the following format:

{ date: <dateExpression>, timezone: <timezoneExpression> }

This allows to apply timezone-aware date operations.

- `<tzExpression>`: (optional) The timezone of the operation result. It must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If no timezone is provided, the result is in UTC.

Example (MongoDB Shell)

The following example demonstrates how to use the $hour operator to extract the hour component from a date field and group the data accordingly.

Create sample documents

db.events.insertMany([ { timestamp: new Date("2023-04-01T10:30:00Z") }, { timestamp: new Date("2023-04-01T12:45:00Z") }, { timestamp: new Date("2023-04-02T08:15:00Z") }, { timestamp: new Date("2023-04-02T16:20:00Z") }, { timestamp: new Date("2023-04-03T23:59:00Z") } ]);

Query example

db.events.aggregate([ { $project: { hour: { $hour: "$timestamp" } } }, { $group: { _id: "$hour", count: { $sum: 1 } } }, { $sort: { _id: 1 } } ]);

Output

[ { "_id": 8, "count": 1 }, { "_id": 10, "count": 1 }, { "_id": 12, "count": 1 }, { "_id": 16, "count": 1 }, { "_id": 23, "count": 1 } ]

This query groups the events by the hour component of the timestamp field and counts the number of events for each hour.

Code examples

To view a code example for using the $hour command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const collection = db.collection('events'); const result = await collection.aggregate([ { $project: { hour: { $hour: "$timestamp" } } }, { $group: { _id: "$hour", count: { $sum: 1 } } }, { $sort: { _id: 1 } } ]).toArray(); console.log(result); } catch (error) { console.error('Error occurred:', error); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient from datetime import datetime def example(): try: client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.events result = list(collection.aggregate([ { "$project": { "hour": {"$hour": "$timestamp"} } }, { "$group": { "_id": "$hour", "count": {"$sum": 1} } }, { "$sort": {"_id": 1} } ])) print(result) except Exception as e: print(f"An error occurred: {e}") finally: client.close() example()