This article lists the various aggregation pipeline operators that can be used to return individual date parts from a Date object in MongoDB.
Specific Date Parts
The following table lists the operators that are specific to a particular date part.
Operator | Description |
---|---|
$dayOfMonth | Returns the day of the month for a given date as a number between 1 and 31 . |
$dayOfWeek | Returns the day of the week for a given date as a number between 1 (Sunday) and 7 (Saturday). |
$dayOfYear | Returns the day of the year for a given date as a number between 1 and 366 (leap year). |
$hour | Returns the hour for a given date as a number between 0 and 23 . |
$isoDayOfWeek | Returns the weekday number of a given date in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday). |
$isoWeek | Returns the week number of a given date in ISO 8601 format, ranging from 1 to 53 . |
$isoWeekYear | Returns the year number for a given date in ISO 8601 format. |
$millisecond | Returns the milliseconds of a given date as a number between 0 and 999 . |
$minute | Returns the minute for a given date as a number between 0 and 59 . |
$month | Returns the month for a given date as a number between 1 (January) and 12 (December). |
$second | Returns the seconds for a given date as a number between 0 and 60 (leap seconds). |
$week | Returns the week number for a given date as a number between 0 (the partial week that precedes the first Sunday of the year) and 53 (leap year). |
$year | Returns the year for a given date as a number (e.g. 2021 ). |
More General Operators
The following operators are more general (i.e. you can use them for all date parts).
Operator | Description |
---|---|
$dateToParts | Returns a document containing the constituent parts of a date. |
$dateToString | Returns the date as a string. You can use zero or more format specifiers to return specific date parts. |