On this page本页内容
$dateToParts
Returns a document that contains the constituent parts of a given BSON Date value as individual properties. 返回包含给定BSON日期值的组成部分作为单个属性的文档。The properties returned are 返回的属性为year
, month
, day
, hour
, minute
, second
and millisecond
.year
、month
、day
、hour
、minute
、second
和millisecond
。
You can set the 可以将iso8601
property to true
to return the parts representing an ISO week date instead. iso8601
属性设置为true
,以返回表示ISO周日期的零件。This will return a document where the properties are 这将返回一个文档,其中属性为isoWeekYear
, isoWeek
, isoDayOfWeek
, hour
, minute
, second
and millisecond
.isoWeekYear
、isoWeek
、isoDayOfWeek
、hour
、minute
、second
和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
接受具有以下字段的文档:
date |
| |
timezone |
| |
iso8601 | true , modifies the output document to use ISO week date fields. true ,则修改输出文档以使用ISO周日期字段。false .false 。
|
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:$hour
和$minute
运算符返回日期字段的相应部分:
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 }
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 } }