$dateToParts (aggregation)

Definition

  • $dateToParts

New in version 3.6.

Returns a document that contains the constituent parts of a givenBSON Date value as individual properties. The properties returnedare year, month, day, hour, minute, secondand millisecond.

You can set the iso8601 property to true to return the partsrepresenting anISO week dateinstead. This will return a document where the properties areisoWeekYear, isoWeek, isoDayOfWeek, hour,minute, second and millisecond.

The $dateToParts expression has the following syntax:

  1. {
  2. $dateToParts: {
  3. 'date' : <dateExpression>,
  4. 'timezone' : <timezone>,
  5. 'iso8601' : <boolean>
  6. }
  7. }

The $dateToParts takes a document with the following fields:

FieldRequired/OptionalDescriptiondateRequired

Changed in version 3.6.

The input date for which to return parts.<dateExpression> can be anyexpression that resolves to aDate, aTimestamp, or anObjectID. For moreinformation on expressions, seeExpressions.timezoneOptionalThe timezone to use to format the date. By default,$dateToParts uses UTC.

<timezone> can be any expression that evaluates to a string whosevalue is either:

  • an Olson Timezone Identifier,such as "Europe/London" or "America/New_York", or
  • a UTC offset in the form:
    • +/-[hh]:[mm], e.g. "+04:45", or
    • +/-[hh][mm], e.g. "-0530", or
    • +/-[hh], e.g. "+03".For more information on expressions, seeExpressions.iso8601OptionalIf set to true, modifies the output document to use ISOweek date fields. Defaults to false.

Behavior

When using an Olson Timezone Identifier in the <timezone>field, MongoDB applies the DST offsetif applicable for the specified timezone.

For example, consider a sales collection with the following document:

  1. {
  2. "_id" : 1,
  3. "item" : "abc",
  4. "price" : 20,
  5. "quantity" : 5,
  6. "date" : ISODate("2017-05-20T10:24:51.303Z")
  7. }

The following aggregation illustrates how MongoDB handles the DSToffset for the Olson Timezone Identifier. The example uses the$hour and $minute operators to return thecorresponding portions of the date field:

  1. db.sales.aggregate([
  2. {
  3. $project: {
  4. "nycHour": {
  5. $hour: { date: "$date", timezone: "-05:00" }
  6. },
  7. "nycMinute": {
  8. $minute: { date: "$date", timezone: "-05:00" }
  9. },
  10. "gmtHour": {
  11. $hour: { date: "$date", timezone: "GMT" }
  12. },
  13. "gmtMinute": {
  14. $minute: { date: "$date", timezone: "GMT" } },
  15. "nycOlsonHour": {
  16. $hour: { date: "$date", timezone: "America/New_York" }
  17. },
  18. "nycOlsonMinute": {
  19. $minute: { date: "$date", timezone: "America/New_York" }
  20. }
  21. }
  22. }])

The operation returns the following result:

  1. {
  2. "_id": 1,
  3. "nycHour" : 5,
  4. "nycMinute" : 24,
  5. "gmtHour" : 10,
  6. "gmtMinute" : 24,
  7. "nycOlsonHour" : 6,
  8. "nycOlsonMinute" : 24
  9. }

Example

Consider a sales collection with the following document:

  1. {
  2. "_id" : 2,
  3. "item" : "abc",
  4. "price" : 10,
  5. "quantity" : 2,
  6. "date" : ISODate("2017-01-01T01:29:09.123Z")
  7. }

The following aggregation uses $dateToParts to return adocument that contains the constituent parts of the date field.

  1. db.sales.aggregate([
  2. {
  3. $project: {
  4. date: {
  5. $dateToParts: { date: "$date" }
  6. },
  7. date_iso: {
  8. $dateToParts: { date: "$date", iso8601: true }
  9. },
  10. date_timezone: {
  11. $dateToParts: { date: "$date", timezone: "America/New_York" }
  12. }
  13. }
  14. }])

The operation returns the following result:

  1. {
  2. "_id" : 2,
  3. "date" : {
  4. "year" : 2017,
  5. "month" : 1,
  6. "day" : 1,
  7. "hour" : 1,
  8. "minute" : 29,
  9. "second" : 9,
  10. "millisecond" : 123
  11. },
  12. "date_iso" : {
  13. "isoWeekYear" : 2016,
  14. "isoWeek" : 52,
  15. "isoDayOfWeek" : 7,
  16. "hour" : 1,
  17. "minute" : 29,
  18. "second" : 9,
  19. "millisecond" : 123
  20. },
  21. "date_timezone" : {
  22. "year" : 2016,
  23. "month" : 12,
  24. "day" : 31,
  25. "hour" : 20,
  26. "minute" : 29,
  27. "second" : 9,
  28. "millisecond" : 123
  29. }
  30. }