Docs Menu
Docs Home
/
MongoDB マニュアル
/ / /

$toDate(集計)

項目一覧

  • 定義
  • 動作
$toDate

値を日付に変換します。 値を日付に変換できない場合は、 $toDateがエラーになります。 値が null または欠落している場合、 $toDateは null を返します。

$toDate の構文は次のとおりです。

{
$toDate: <expression>
}

$toDateは任意の有効な式を受け入れます。

$toDate は次の $convert 式の省略形です。

{ $convert: { input: <expression>, to: "date" } }

Tip

以下も参照してください。

  • $convert

  • $dateFromString

次の表で、日付に変換できる入力型を一覧にしています。

入力タイプ
動作
Double

切り捨てられた double 値で表されるミリ秒数に対応する日付を返します。

正の数は、1970 年 1 月 1 日からのミリ秒数に対応します。

負の数は、1970 年 1 月 1 日より前のミリ秒数に対応します。

小数点

切り捨てられた小数値で表されるミリ秒数に対応する日付を返します。

正の数は、1970 年 1 月 1 日からのミリ秒数に対応します。

負の数は、1970 年 1 月 1 日より前のミリ秒数に対応します。

Long

long 値で表されるミリ秒数に対応する日付を返します。

正の数は、1970 年 1 月 1 日からのミリ秒数に対応します。

負の数は、1970 年 1 月 1 日より前のミリ秒数に対応します。

文字列

日付文字列 に対応する日付を返します。

文字列は次のような有効な日付文字列でなければなりません。

  • "2018-03-20"

  • "2018-03-20T12:00:00Z"

  • "2018-03-20T12:00:00+0500"

ObjectId
ObjectId のタイムスタンプに対応する日付を返します。
タイムスタンプ
タイムスタンプに対応する日付を返します。

次の表で、日付への変換例の一部を示します。

結果
{$toDate: 120000000000.5}
ISODate("1973-10-20T21:20:00Z")
{$toDate: NumberDecimal("1253372036000.50")}
ISODate("2009-09-19T14:53:56Z")
{$toDate: NumberLong("1100000000000")}
ISODate("2004-11-19T11:33:20Z")
{$toDate: NumberLong("-1100000000000")}
ISODate("1935-02-22T12:26:40Z")
{$toDate: ObjectId("5ab9c3da31c2ab715d421285")}
ISODate( "2018-03-27T04:08:58Z")
{$toDate: "2018-03-20"}
ISODate("2018-03-20T00:00:00Z")
{$toDate: "2018-03-20 11:00:06 +0500"}
ISODate("2018-03-20T06:00:06Z")
{$toDate: "Friday"}
エラー
{$toDate: Timestamp({ t: 1637688118, i: 1 })}
ISODate( "2021-11-23T17:21:58.00Z")

次のドキュメントを使用してコレクション orders を作成します。

db.orders.insertMany( [
{ _id: 1, item: "apple", qty: 5, price: 2, order_date: new Date( "2018-03-20" ) },
{ _id: 2, item: "pie", qty: 10, price: 3, order_date: new Date( "2018-03-22" ) },
{ _id: 3, item: "ice cream", qty: 2, price: 4, order_date: "2018-03-15" },
{ _id: 4, item: "almonds" , qty: 5, price: 7, order_date: "2018-03-15 +10:00" }
] )

orders コレクションに対する次の集計操作では、日付値で並べ替える前に order_date を日付に変換します。

// Define stage to add convertedDate field with the converted order_date value
dateConversionStage = {
$addFields: {
convertedDate: { $toDate: "$order_date" }
}
};
// Define stage to sort documents by the converted date
sortStage = {
$sort: { "convertedDate": 1 }
};
db.orders.aggregate( [
dateConversionStage,
sortStage
] )

この操作により、次のドキュメントが返されます。

{
_id: 4,
item: 'almonds',
qty: 5,
price: 7,
order_date: '2018-03-15 +10:00',
convertedDate: ISODate("2018-03-14T14:00:00.000Z")
},
{
_id: 3,
item: 'ice cream',
qty: 2,
price: 4,
order_date: '2018-03-15',
convertedDate: ISODate("2018-03-15T00:00:00.000Z")
},
{
_id: 1,
item: 'apple',
qty: 5,
price: 2,
order_date: ISODate("2018-03-20T00:00:00.000Z"),
convertedDate: ISODate("2018-03-20T00:00:00.000Z")
},
{
_id: 2,
item: 'pie',
qty: 10,
price: 3,
order_date: ISODate("2018-03-22T00:00:00.000Z"),
convertedDate: ISODate("2018-03-22T00:00:00.000Z")
}

注意

変換操作でエラーが発生した場合、集計操作は停止し、エラーがスローされます。この動作をオーバーライドするには、代わりに $convert を使用します。

戻る

$toBool

項目一覧