$dayOfMonth - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$dayOfMonth

The $dayOfMonth aggregation operator in Amazon DocumentDB retrieves the day of the month (from 1 to 31) for a given date. This operator is useful for grouping, filtering, or extracting the day of the month from date fields in your documents.

Parameters

  • date expression: The date expression can be a date field from the document, a date object, or a date string.

Example (MongoDB Shell)

This example demonstrates how to use the $dayOfMonth operator to extract the day of the month from a date field in the document.

Create sample documents

db.events.insertMany([ { _id: 1, eventDate: new Date("2022-01-15T12:00:00Z") }, { _id: 2, eventDate: new Date("2022-02-28T15:30:00Z") }, { _id: 3, eventDate: new Date("2022-03-01T09:45:00Z") }, { _id: 4, eventDate: new Date("2022-04-30T23:59:59Z") } ]);

Query example

db.events.aggregate([ { $project: { eventDay: { $dayOfMonth: "$eventDate" } }} ])

Output

[ { "_id" : 1, "eventDay" : 15 }, { "_id" : 2, "eventDay" : 28 }, { "_id" : 3, "eventDay" : 1 }, { "_id" : 4, "eventDay" : 30 } ]

Code examples

To view a code example for using the $dayOfMonth 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 events = db.collection('events'); const result = await events.aggregate([ { $project: { eventDay: { $dayOfMonth: "$eventDate" } }} ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.mydatabase events = db.events result = list(events.aggregate([ { "$project": { "eventDay": { "$dayOfMonth": "$eventDate" } }} ])) print(result) client.close() example()