定義
$dateFromString日付/時刻文字列をオブジェクトに変換します。
The
$dateFromStringexpression has the following syntax:{ $dateFromString: { dateString: <dateStringExpression>, format: <formatStringExpression>, timezone: <tzExpression>, onError: <onErrorExpression>, onNull: <onNullExpression> } } The
$dateFromStringtakes a document with the following fields:フィールド説明dateString日付オブジェクトに変換する日付や時刻の文字列。日付や時刻の形式の詳細については、
Date()を参照してください。演算子に
timezoneオプションを指定する場合は、dateStringにタイムゾーン情報を含めないでください。formatOptional. The date format specification of the
dateString. Theformatcan be any expression that evaluates to a string literal, containing 0 or more format specifiers. 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.timezone任意。日付の形式に使用するタイムゾーン。
dateString引数が "2017-02-08T12:10:40.787Z" のように形式されている場合(末尾の "Z" は協定世界時(UTC タイムゾーン)を表します)、timezone引数は指定できません。<timezone>次のオプションとそれを評価する式が使用できます。Olson タイムゾーン識別子(
"Europe/London"や"America/New_York"など)、または次の形式の UTC オフセット:
+/-[hh]:[mm]、例:"+04:45"、または+/-[hh][mm]、例:"-0530"、または+/-[hh]、例:"+03"、または
文字列
"Z"、"UTC"、または"GMT"
式の詳細については、「式 」を参照してください。
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.If you do not specify
onError,$dateFromStringthrows an error if it cannot parsedateString.onNullOptional. If the
dateStringprovided to$dateFromStringisnullor missing, it outputs the result value of the providedonNullexpression. This result value can be of any type.If you do not specify
onNullanddateStringisnullor missing, then$dateFromStringoutputsnull.
動作
例 | 結果 | ||||
|---|---|---|---|---|---|
|
| ||||
|
| ||||
|
| ||||
|
| ||||
|
| ||||
|
| ||||
|
|
フォーマット指定子
<formatString>0で使える書式指定子は以下の通りでし。
指定子 | 説明 | Possible Values |
|---|---|---|
| 省略表記の月(3 文字) |
|
| 正式な月名 |
|
| 日付(2桁、ゼロあり) |
|
| ISO 8601 形式の年 |
|
| 時間(2 桁、ゼロあり、24 時間表記) |
|
| 年内の日(3桁、ゼロ埋め) |
|
| ミリ秒 (3 桁、ゼロ埋め込み) |
|
| 月 (2 桁、ゼロ埋め込み) |
|
| 分(2桁、ゼロ埋め込み) |
|
| 秒(2 桁、ゼロ埋め込み) |
|
| ISO 8601 形式の曜日番号(1 - 月曜日、7 - 日曜日) |
|
| 年内の週(2 桁、ゼロ埋め込み) |
|
| ISO 8601形式の年内の週 |
|
| 整数で表された曜日(0:日曜日、6:土曜日) |
|
| 年(4 桁、ゼロ埋め込み) |
|
| UTC からのタイムゾーン オフセット。 |
|
| UTC からのオフセットを数値として表した分。たとえば、タイムゾーン オフセット ( |
|
| リテラルパーセント文字 |
|
例
日付の変換
以下の日付入りドキュメントを含むコレクション 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"}
次の集計では、$dateFromString を使用して date 値を日付オブジェクトに変換します。
db.logmessages.aggregate( [ { $project: { date: { $dateFromString: { dateString: '$date', timezone: 'America/New_York' } } } } ] )
上記の集計により、次のドキュメントが返され、各 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") }
ハードコードされた引数の代わりに、ドキュメント フィールドを介してtimezone 引数も利用できます。たとえば次のとおりです。
db.logmessages.aggregate( [ { $project: { date: { $dateFromString: { dateString: '$date', timezone: '$timezone' } } } } ] )
上記の集計により、次のドキュメントが返され、各 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.
たとえば、次のドキュメントが含まれたコレクション 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" }
無効な日付を元の文字列形式で返すには、 onError パラメータを使用します。
db.dates.aggregate( [ { $project: { date: { $dateFromString: { dateString: '$date', timezone: '$timezone', onError: '$date' } } } } ] )
これにより、次のドキュメントが返されます。
{ "_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.
たとえば、次のドキュメントが含まれたコレクション 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:
db.dates.aggregate( [ { $project: { date: { $dateFromString: { dateString: '$date', timezone: '$timezone', onNull: new Date(0) } } } } ] )
これにより、次のドキュメントが返されます。
{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") } { "_id" : 2, "date" : ISODate("1970-01-01T00:00:00Z") }