$minute - Amazon DocumentDB

$minute

The $minute aggregation pipeline stage in Amazon DocumentDB extracts the minute value from a date or timestamp field.

This operator is useful when you need to perform date and time-based calculations or grouping within your aggregation pipeline.

Parameters

  • expression: The date or timestamp field from which to extract the minute value.

Example (MongoDB Shell)

The following example demonstrates how to use the $minute operator to group the documents by the minute value extracted from the timestamp field and count the number of documents in each group.

Create sample documents

db.events.insertMany([ { timestamp: new Date("2023-04-15T10:30:25.000Z") }, { timestamp: new Date("2023-04-15T10:30:35.000Z") }, { timestamp: new Date("2023-04-15T10:31:05.000Z") }, { timestamp: new Date("2023-04-15T10:31:45.000Z") }, { timestamp: new Date("2023-04-15T10:32:15.000Z") } ]);

Query example

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

Output

[ { "_id": { "minute": 30 }, "count": 2 }, { "_id": { "minute": 31 }, "count": 2 }, { "_id": { "minute": 32 }, "count": 1 } ]

Code examples

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

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('events'); await collection.insertMany([ { timestamp: new Date("2023-04-15T10:30:25.000Z") }, { timestamp: new Date("2023-04-15T10:30:35.000Z") }, { timestamp: new Date("2023-04-15T10:31:05.000Z") }, { timestamp: new Date("2023-04-15T10:31:45.000Z") }, { timestamp: new Date("2023-04-15T10:32:15.000Z") } ]); const result = await collection.aggregate([ { $group: { _id: { minute: { $minute: "$timestamp" } }, count: { $count: {} } } }, { $sort: { "_id.minute": 1 } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient from datetime import datetime def example(): 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'] collection.insert_many([ {'timestamp': datetime(2023, 4, 15, 10, 30, 25)}, {'timestamp': datetime(2023, 4, 15, 10, 30, 35)}, {'timestamp': datetime(2023, 4, 15, 10, 31, 5)}, {'timestamp': datetime(2023, 4, 15, 10, 31, 45)}, {'timestamp': datetime(2023, 4, 15, 10, 32, 15)} ]) result = list(collection.aggregate([ { '$group': { '_id': { 'minute': {'$minute': '$timestamp'} }, 'count': {'$count': {}} } }, {'$sort': {'_id.minute': 1}} ])) print(result) client.close() example()