$dateToParts (aggregation)

On this page本页内容

Definition定义

$dateToParts

New in version 3.6.版本3.6中的新功能。

Returns a document that contains the constituent parts of a given BSON Date value as individual properties. 返回一个文档,该文档包含给定BSON Date值的组成部分,作为单个属性。The properties returned are year, month, day, hour, minute, second and millisecond.返回的属性为yearmonthdayhourminutesecondmillisecond

You can set the iso8601 property to true to return the parts representing an ISO week date instead. This will return a document where the properties are isoWeekYear, isoWeek, isoDayOfWeek, hour, minute, second and millisecond.

The $dateToParts expression has the following syntax:$dateToParts表达式语法如下所示:

{
    $dateToParts: {
        'date' : <dateExpression>,
        'timezone' : <timezone>,
        'iso8601' : <boolean>
    }
}

The $dateToParts takes a document with the following fields:$dateToParts采用带有以下字段的文档

Field字段Required/OptionalDescription描述
date Required

Changed in version 3.6.在版本3.6中更改。

The input date for which to return parts. 返回零件的输入日期。<dateExpression> can be any expression that resolves to a Date, a Timestamp, or an ObjectID. For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

timezone Optional

The timezone to use to format the date. 用于设置日期格式的时区。By default, $dateToParts uses UTC.默认情况下,$dateToParts使用UTC。

<timezone> can be any expression that evaluates to a string whose value 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, see Expressions.有关表达式的详细信息,请参阅表达式

iso8601 Optional可选 If set to true, modifies the output document to use ISO week date fields. 如果设置为true,则修改输出文档以使用ISO周日期字段。Defaults to false.默认为false

Behavior行为

When using an Olson Timezone Identifier in the <timezone> field, MongoDB applies the DST offset if applicable for the specified timezone.<Timezone>字段中使用奥尔森时区标识符时,MongoDB会在适用于指定时区的情况下应用DST偏移量。

For example, consider a sales collection with the following document:例如,考虑带有以下文档的sales集合:

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

The following aggregation illustrates how MongoDB handles the DST offset for the Olson Timezone Identifier. 下面的聚合说明了MongoDB如何处理Olson时区标识符的DST偏移量。The example uses the $hour and $minute operators to return the corresponding portions of the date field:

db.sales.aggregate([
{
   $project: {
      "nycHour": {
         $hour: { date: "$date", timezone: "-05:00" }
       },
       "nycMinute": {
          $minute: { date: "$date", timezone: "-05:00" }
       },
       "gmtHour": {
          $hour: { date: "$date", timezone: "GMT" }
       },
       "gmtMinute": {
          $minute: { date: "$date", timezone: "GMT" } },
       "nycOlsonHour": {
          $hour: { date: "$date", timezone: "America/New_York" }
       },
       "nycOlsonMinute": {
          $minute: { date: "$date", timezone: "America/New_York" }
       }
   }
}])

The operation returns the following result:操作返回以下结果:

{
   "_id": 1,
   "nycHour" : 5,
   "nycMinute" : 24,
   "gmtHour" : 10,
   "gmtMinute" : 24,
   "nycOlsonHour" : 6,
   "nycOlsonMinute" : 24
}

Example示例

Consider a sales collection with the following document:考虑一个sales集合带有以下文档:

{
  "_id" : 2,
  "item" : "abc",
  "price" : 10,
  "quantity" : 2,
  "date" : ISODate("2017-01-01T01:29:09.123Z")
}

The following aggregation uses $dateToParts to return a document that contains the constituent parts of the date field.以下聚合使用$dateToParts返回包含date字段组成部分的文档。

 db.sales.aggregate([
 {
    $project: {
       date: {
          $dateToParts: { date: "$date" }
       },
       date_iso: {
          $dateToParts: { date: "$date", iso8601: true }
       },
       date_timezone: {
          $dateToParts: { date: "$date", timezone: "America/New_York" }
       }
    }
}])

The operation returns the following result:操作返回以下结果:

{
   "_id" : 2,
   "date" : {
      "year" : 2017,
      "month" : 1,
      "day" : 1,
      "hour" : 1,
      "minute" : 29,
      "second" : 9,
      "millisecond" : 123
   },
   "date_iso" : {
      "isoWeekYear" : 2016,
      "isoWeek" : 52,
      "isoDayOfWeek" : 7,
      "hour" : 1,
      "minute" : 29,
      "second" : 9,
      "millisecond" : 123
   },
   "date_timezone" : {
      "year" : 2016,
      "month" : 12,
      "day" : 31,
      "hour" : 20,
      "minute" : 29,
      "second" : 9,
      "millisecond" : 123
   }
}