定义
$dateFromParts根据日期的组成属性,构造并返回日期对象。
The
$dateFromPartsexpression has the following syntax:{ $dateFromParts : { 'year': <year>, 'month': <month>, 'day': <day>, 'hour': <hour>, 'minute': <minute>, 'second': <second>, 'millisecond': <ms>, 'timezone': <tzExpression> } } 您还可以使用以下事务语法,以 ISO 周日期格式指定组成日期字段:
{ $dateFromParts : { 'isoWeekYear': <year>, 'isoWeek': <week>, 'isoDayOfWeek': <day>, 'hour': <hour>, 'minute': <minute>, 'second': <second>, 'millisecond': <ms>, 'timezone': <tzExpression> } } The
$dateFromPartstakes a document with the following fields:重要
You cannot combine the use of calendar dates and ISO week date fields when constructing your
$dateFromPartsinput document.字段必需/可选说明year必填字段,前提是不使用
isoWeekYearisoWeekYear必填字段,前提是不使用
yearmonth可选。只能与
year一起使用。isoWeek可选。只能与
isoWeekYear一起使用。day可选。只能与
year一起使用。isoDayOfWeek可选。只能与
isoWeekYear一起使用。hourOptional
minuteOptional
secondOptional
millisecondOptional
timezoneOptional
<timezone>可以是任何计算结果为字符串的表达式,其值为:Olson 时区标识符,例如
"Europe/London"或"America/New_York",或者UTC 偏移量,格式如下:
+/-[hh]:[mm],例如"+04:45",或+/-[hh][mm],例如"-0530",或+/-[hh], e.g."+03".
有关表达式的更多信息,请参阅表达式。
行为
数值范围
year 和 isoWeekYear 支持的值范围是 1-9999。
If the value specified for fields other than year, isoWeekYear, and timezone is outside the valid range, $dateFromParts carries or subtracts the difference from other date parts to calculate the date.
值大于范围
Consider the following $dateFromParts expression where the month field value is 14, which is 2 months greater than the maximum value of 12 months(or 1 year):
{ $dateFromParts: { 'year' : 2017, 'month' : 14, 'day': 1, 'hour' : 12 } }
该表达式通过将 year 增加 1 并将 month 设置为 2 来计算日期,返回:
ISODate("2018-02-01T12:00:00Z")
值小于范围
Consider the following $dateFromParts expression where the month field value is 0, which is 1 month less than the minimum value of 1 month:
{ $dateFromParts: { 'year' : 2017, 'month' : 0, 'day': 1, 'hour' : 12 } }
该表达式通过将 year 减少 1 并将 month 设置为 12 来计算日期,返回:
ISODate("2016-12-01T12:00:00Z")
时区
在 <timezone> 字段中使用 Olson 时区标识符时,如果适用于指定的时区,MongoDB 会应用 DST 偏移量。
例如,考虑包含以下文档的 sales 集合:
db.sales.insertOne( { "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:15:39.736Z") } )
以下聚合说明了 MongoDB 如何处理 Olson 时区标识符的 DST 偏移量。该示例使用 $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" } } } }])
操作返回以下结果:
{ "_id": 1, "nycHour" : 5, "nycMinute" : 24, "gmtHour" : 10, "gmtMinute" : 24, "nycOlsonHour" : 6, "nycOlsonMinute" : 24 }
例子
The following aggregation uses $dateFromParts to construct three date objects from the provided input fields:
db.sales.aggregate([ { $project: { date: { $dateFromParts: { 'year' : 2017, 'month' : 2, 'day': 8, 'hour' : 12 } }, date_iso: { $dateFromParts: { 'isoWeekYear' : 2017, 'isoWeek' : 6, 'isoDayOfWeek' : 3, 'hour' : 12 } }, date_timezone: { $dateFromParts: { 'year' : 2016, 'month' : 12, 'day' : 31, 'hour' : 23, 'minute' : 46, 'second' : 12, 'timezone' : 'America/New_York' } } } }])
操作返回以下结果:
{ "_id" : 1, "date" : ISODate("2017-02-08T12:00:00Z"), "date_iso" : ISODate("2017-02-08T12:00:00Z"), "date_timezone" : ISODate("2017-01-01T04:46:12Z") }