$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: