Definition定义
$dateToPartsReturns 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,secondandmillisecond.year、month、day、hour、minute、second和millisecond。You can set the您可以将iso8601property totrueto 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,secondandmillisecond.isoWeekYear、isoWeek、isoDayOfWeek、hour、minute、second和millisecond。The$dateToPartsexpression has the following syntax:$dateToParts表达式具有以下语法:{
$dateToParts: {
'date' : <dateExpression>,
'timezone' : <timezone>,
'iso8601' : <boolean>
}
}The$dateToPartstakes a document with the following fields:$dateToParts接受具有以下字段的文档:Field字段Required/Optional必需/可选Description描述dateRequired必需The input date for which to return parts.返回零件的输入日期。<dateExpression>can be any expression that resolves to a Date, a Timestamp, or an ObjectID.<dateExpression>可以是解析为Date、Timestamp或ObjectID的任何表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。timezoneOptional可选The timezone to use to format the date. By default,用于格式化日期的时区。默认情况下,$dateToPartsuses 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"Europe/London"或"America/New_York",或a UTC offset in the form:UTC偏移量,格式为:+/-[hh]:[mm], e.g.,例如"+04:45", or"+04:45",或者+/-[hh][mm], e.g.,例如"-0530", or"-0530",或者+/-[hh], e.g.,例如"+03"."+03"。
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。iso8601Optional可选If set to如果设置为true, modifies the output document to use ISO week date fields. Defaults tofalse.true,则修改输出文档以使用ISO周日期字段。默认为false。
Behavior行为
When using an Olson Timezone Identifier in the 在<timezone> field, MongoDB applies the DST offset if applicable for the specified timezone.<Timezone>字段中使用Olson时区标识符时,MongoDB会应用DST偏移(如果适用于指定的时区)。
For example, consider a 例如,考虑具有以下文档的sales collection with the following document:sales集合:
db.sales.insertOne(
{
"_id" : 1,
"item" : "abc",
"price" : 10,
"quantity" : 2,
"date" : ISODate("2014-01-01T08:15:39.736Z")
}
)
The following aggregation illustrates how MongoDB handles the DST offset for the Olson Timezone Identifier. The example uses the 以下聚合说明了MongoDB如何处理Olson时区标识符的DST偏移。该示例使用$hour and $minute operators to return the corresponding portions of the date field:$hour和$minute运算符返回date字段的相应部分:
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
}
}