AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$dateFromString(式演算子)

$dateFromString

日付/時刻文字列をオブジェクトに変換します。

The $dateFromString expression has the following syntax:

{ $dateFromString: {
dateString: <dateStringExpression>,
format: <formatStringExpression>,
timezone: <tzExpression>,
onError: <onErrorExpression>,
onNull: <onNullExpression>
} }

The $dateFromString takes a document with the following fields:

フィールド
説明

dateString

日付オブジェクトに変換する日付や時刻の文字列。日付や時刻の形式の詳細については、Date() を参照してください。

演算子に timezone オプションを指定する場合は、dateString にタイムゾーン情報を含めないでください。

format

Optional. The date format specification of the dateString. The format can 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, $dateFromString uses "%Y-%m-%dT%H:%M:%S.%LZ" as the default format but accepts a variety of formats and attempts to parse the dateString if 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"

式の詳細については、「式 」を参照してください。

onError

Optional. If $dateFromString encounters an error while parsing the given dateString, it outputs the result value of the provided onError expression. This result value can be of any type.

If you do not specify onError, $dateFromString throws an error if it cannot parse dateString.

onNull

Optional. If the dateString provided to $dateFromString is null or missing, it outputs the result value of the provided onNull expression. This result value can be of any type.

If you do not specify onNull and dateString is null or missing, then $dateFromString outputs null.

例
結果
{ $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: "oct 20 2020"
} }

ISODate("2020-10-20T00:00:00.000Z")

{ $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")

{ $dateFromString: {
dateString: "WED jan 31 12:05:28 +03:30 1996"
} }

ISODate("1996-01-31T08:35:28.000Z")

<formatString>0で使える書式指定子は以下の通りでし。

指定子
説明
Possible Values

%b

省略表記の月(3 文字)

jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec

%B

正式な月名

january-december

%d

日付(2桁、ゼロあり)

01-31

%G

ISO 8601 形式の年

0000-9999

%H

時間(2 桁、ゼロあり、24 時間表記)

00-23

%j

年内の日(3桁、ゼロ埋め)

001-366

%L

ミリ秒 (3 桁、ゼロ埋め込み)

000-999

%m

月 (2 桁、ゼロ埋め込み)

01-12

%M

分(2桁、ゼロ埋め込み)

00-59

%S

秒(2 桁、ゼロ埋め込み)

00-60

%u

ISO 8601 形式の曜日番号(1 - 月曜日、7 - 日曜日)

1-7

%U

年内の週(2 桁、ゼロ埋め込み)

00-53

%V

ISO 8601形式の年内の週

1-53

%w

整数で表された曜日(0:日曜日、6:土曜日)

0-6

%Y

年(4 桁、ゼロ埋め込み)

0000-9999

%z

UTC からのタイムゾーン オフセット。

+/-[hh][mm]

%Z

UTC からのオフセットを数値として表した分。たとえば、タイムゾーン オフセット ( +/-[hhmm] ) が+0445の場合、分オフセットは+285になります。

+/-mmm

%%

リテラルパーセント文字

%

以下の日付入りドキュメントを含むコレクション 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") }

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" }

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") }