On this page本页内容
$dateFromString
Converts a date/time string to a date object.将日期/时间字符串转换为日期对象。
The $dateFromString
expression has the following syntax:$dateFromString
表达式语法如下:
{ $dateFromString: { dateString: <dateStringExpression>, format: <formatStringExpression>, timezone: <tzExpression>, onError: <onErrorExpression>, onNull: <onNullExpression> } }
The $dateFromString
takes a document with the following fields:$dateFromString
接受具有以下字段的文档:
dateString |
|
format |
|
timezone |
|
onError |
|
onNull |
|
{ $dateFromString: { dateString: "2017-02-08T12:10:40.787" } } | ISODate("2017-02-08T12:10:40.787Z") |
{ $dateFromString: { dateString: "2017-02-08T12:10:40.787", timezone: "America/New_York" } } | ISODate("2017-02-08T17:10:40.787Z") |
{ $dateFromString: { dateString: "2017-02-08" } } | ISODate("2017-02-08T00:00:00Z") |
{ $dateFromString: { dateString: "06-15-2018", format: "%m-%d-%Y" } } | ISODate("2018-06-15T00:00:00Z") |
{ $dateFromString: { dateString: "15-06-2018", format: "%d-%m-%Y" } } | ISODate("2018-06-15T00:00:00Z") |
The following format specifiers are available for use in the 以下格式说明符可用于<formatString>
:<formatString>
:
%d | 01 -31 | |
%G | 0000 -9999 | |
%H | 00 -23 | |
%L | 000 -999 | |
%m | 01 -12 | |
%M | 00 -59 | |
%S | 00 -60 | |
%u | 1 -7 | |
%V | 1 -53 | |
%Y | 0000 -9999 | |
%z | +/-[hh][mm] | |
%Z | +/-[hhmm] ) was +0445 , the minutes offset is +285 .+/-[hhmm] )为+0445 ,则分钟偏移量为+285 。
| +/-mmm |
%% | % |
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. timezone
参数也可以通过文档字段而不是硬编码参数提供。For example:例如:
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.$dateFromString
将抛出错误,除非您为可选的onError
参数提供聚合表达式。
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
日期字符串的文档,$dateFromString
将返回null
,除非为可选的onNull
参数提供聚合表达式。
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") }