MongoDB Aggregation Operators for Returning Date Parts

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.

OperatorDescription
$dayOfMonthReturns the day of the month for a given date as a number between 1 and 31.
$dayOfWeekReturns the day of the week for a given date as a number between 1 (Sunday) and 7 (Saturday).
$dayOfYearReturns the day of the year for a given date as a number between 1 and 366 (leap year).
$hourReturns the hour for a given date as a number between 0 and 23.
$isoDayOfWeekReturns the weekday number of a given date in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).
$isoWeekReturns the week number of a given date in ISO 8601 format, ranging from 1 to 53.
$isoWeekYearReturns the year number for a given date in ISO 8601 format.
$millisecondReturns the milliseconds of a given date as a number between 0 and 999.
$minuteReturns the minute for a given date as a number between 0 and 59.
$monthReturns the month for a given date as a number between 1 (January) and 12 (December).
$secondReturns the seconds for a given date as a number between 0 and 60 (leap seconds).
$weekReturns 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).
$yearReturns 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).

OperatorDescription
$dateToPartsReturns a document containing the constituent parts of a date.
$dateToStringReturns the date as a string. You can use zero or more format specifiers to return specific date parts.