Definition定义
$dateFromStringConverts a date/time string to a date object.将日期/时间字符串转换为日期对象。The$dateFromStringexpression has the following syntax:$dateFromString表达式具有以下语法:{ $dateFromString: {
dateString: <dateStringExpression>,
format: <formatStringExpression>,
timezone: <tzExpression>,
onError: <onErrorExpression>,
onNull: <onNullExpression>
} }The$dateFromStringtakes a document with the following fields:$dateFromString接受具有以下字段的文档:Field字段Description描述dateStringThe date/time string to convert to a date object. See要转换为日期对象的日期/时间字符串。有关日期/时间格式的更多信息,请参阅Date()for more information on date/time formats.Date()。If specifying the如果向运算符指定timezoneoption to the operator, do not include time zone information in thedateString.timezone选项,请不要在dateString中包含时区信息。formatOptional.可选。The date format specification of thedateString. Theformatcan be any expression that evaluates to a string literal, containing 0 or more format specifiers.dateString的日期格式规范。format可以是任何计算结果为字符串文字的表达式,包含0个或多个格式说明符。For a list of specifiers available, see Format Specifiers.有关可用说明符的列表,请参阅格式说明符。If unspecified,如果未指定,$dateFromStringuses"%Y-%m-%dT%H:%M:%S.%LZ"as the default format but accepts a variety of formats and attempts to parse thedateStringif possible.$dateFromString将使用"%Y-%m-%dT%H:%M:%S.%LZ"作为默认格式,但接受多种格式,并在可能的情况下尝试解析dateString。timezoneOptional.可选。The time zone to use to format the date.用于格式化日期的时区。If the如果dateStringargument is formatted like '2017-02-08T12:10:40.787Z', in which the 'Z' at the end indicates Zulu time (UTC time zone), you cannot specify thetimezoneargument.dateString参数的格式类似于'2017-02-08T12:10:40.787Z',其中末尾的'Z'表示祖鲁时间(UTC时区),则无法指定时区参数。<timezone>allows for the following options and expressions that evaluate to them:允许使用以下选项和表达式: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", or"+03",或者
The strings字符串"Z","UTC", or"GMT""Z"、"UTC"或"GMT"
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。onErrorOptional.可选。If如果$dateFromStringencounters an error while parsing the givendateString, it outputs the result value of the providedonErrorexpression. This result value can be of any type.$dateFromString在解析给定的dateString时遇到错误,它将输出提供的onError表达式的结果值。此结果值可以是任何类型。If you do not specify如果不指定onError,$dateFromStringthrows an error if it cannot parsedateString.onError,如果$dateFromString无法解析dateString,则会抛出错误。onNullOptional.可选。If the如果提供给dateStringprovided to$dateFromStringisnullor missing, it outputs the result value of the providedonNullexpression. This result value can be of any type.$dateFromString的dateString为null或缺失,它将输出提供的onNull表达式的结果值。此结果值可以是任何类型。If you do not specify如果不指定onNullanddateStringisnullor missing, then$dateFromStringoutputsnull.onNull,并且dateString为null或缺失,则$dateFromString输出null。
Behavior行为
| ISODate("2017-02-08T12:10:40.787Z") |
| ISODate("2017-02-08T17:10:40.787Z") |
| ISODate("2017-02-08T00:00:00Z") |
| ISODate("2020-10-20T00:00:00.000Z") |
| ISODate("2018-06-15T00:00:00Z") |
| ISODate("2018-06-15T00:00:00Z") |
| ISODate("1996-01-31T08:35:28.000Z") |
Format Specifiers格式说明符
The following format specifiers are available for use in the 以下格式说明符可用于<formatString>:<formatString>:
| Possible Values | ||
|---|---|---|
%b | jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec | |
%B | january-december | |
%d | 01-31 | |
%G | 0000-9999 | |
%H | 00-23 | |
%j | 001-366 | |
%L | 000-999 | |
%m | 01-12 | |
%M | 00-59 | |
%S | 00-60 | |
%u | 1-7 | |
%U | 00-53 | |
%V | 1-53 | |
%w | 0-6 | |
%Y | 0000-9999 | |
%z | +/-[hh][mm] | |
%Z | +/-[hhmm]) was +0445, the minutes offset is +285.+/-[hhmm])为+0445,则分钟偏移量为+285。 | +/-mmm |
%% | % |
Examples示例
Converting Dates转换日期
Consider a collection 考虑一个包含以下带日期的文档的集合logmessages that contains the following documents with dates.logmessages。
{ _id: 1, date: "2017-02-08T12:10:40.787", timezone: "America/New_York", message: "Step 1: Started" },
{ _id: 2, date: "2017-02-08", timezone: "-05:00", message: "Step 1: Ended" },
{ _id: 3, message: " Step 1: Ended " },
{ _id: 4, date: "2017-02-09", timezone: "Europe/London", message: "Step 2: Started"},
{ _id: 5, date: "2017-02-09T03:35:02.055", timezone: "+0530", message: "Step 2: In Progress"}
The following aggregation uses $dateFromString to convert the 以下聚合使用date value to a date object:$dateFromString将date值转换为日期对象:
db.logmessages.aggregate( [ {
$project: {
date: {
$dateFromString: {
dateString: '$date',
timezone: 'America/New_York'
}
}
}
} ] )
The above aggregation returns the following documents and converts each 上述聚合返回以下文档,并将每个date field to the Eastern Time Zone:date字段转换为东部时区:
{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
{ "_id" : 2, "date" : ISODate("2017-02-08T05:00:00Z") }
{ "_id" : 3, "date" : null }
{ "_id" : 4, "date" : ISODate("2017-02-09T05:00:00Z") }
{ "_id" : 5, "date" : ISODate("2017-02-09T08:35:02.055Z") }
The timezone argument can also be provided through a document field instead of a hard coded argument. For example:timezone参数也可以通过文档字段而不是硬编码参数提供。例如:
db.logmessages.aggregate( [ {
$project: {
date: {
$dateFromString: {
dateString: '$date',
timezone: '$timezone'
}
}
}
} ] )
The above aggregation returns the following documents and converts each 上述聚合返回以下文档,并将每个date field to their respective UTC representations.date字段转换为各自的UTC表示。
{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
{ "_id" : 2, "date" : ISODate("2017-02-08T05:00:00Z") }
{ "_id" : 3, "date" : null }
{ "_id" : 4, "date" : ISODate("2017-02-09T00:00:00Z") }
{ "_id" : 5, "date" : ISODate("2017-02-08T22:05:02.055Z") }onError
If your collection contains documents with unparsable date strings, 如果集合包含具有不可解析日期字符串的文档,除非您为可选的$dateFromString throws an error unless you provide an aggregation expression to the optional onError parameter.onError参数提供聚合表达式,否则$dateFromString将抛出错误。
For example, given a collection 例如,给定一个包含以下文档的集合dates with the following documents:dates:
{ "_id" : 1, "date" : "2017-02-08T12:10:40.787", timezone: "America/New_York" },
{ "_id" : 2, "date" : "20177-02-09T03:35:02.055", timezone: "America/New_York" }
You can use the 您可以使用onError parameter to return the invalid date in its original string form:onError参数以原始字符串形式返回无效日期:
db.dates.aggregate( [ {
$project: {
date: {
$dateFromString: {
dateString: '$date',
timezone: '$timezone',
onError: '$date'
}
}
}
} ] )
This returns the following documents:这将返回以下文档:
{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
{ "_id" : 2, "date" : "20177-02-09T03:35:02.055" }onNull
If your collection contains documents with 如果集合包含日期字符串为null date strings, $dateFromString returns null unless you provide an aggregation expression to the optional onNull parameter.null的文档,除非您为可选的onNull参数提供聚合表达式,否则$dateFromString将返回null。
For example, given a collection 例如,给定一个包含以下文档的集合dates with the following documents:dates:
{ "_id" : 1, "date" : "2017-02-08T12:10:40.787", timezone: "America/New_York" },
{ "_id" : 2, "date" : null, timezone: "America/New_York" }
You can use the 您可以使用onNull parameter to have $dateFromString return a date representing the unix epoch instead of null:onNull参数让$dateFromString返回一个表示unix纪元的日期,而不是null:
db.dates.aggregate( [ {
$project: {
date: {
$dateFromString: {
dateString: '$date',
timezone: '$timezone',
onNull: new Date(0)
}
}
}
} ] )
This returns the following documents:这将返回以下文档:
{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
{ "_id" : 2, "date" : ISODate("1970-01-01T00:00:00Z") }