定義
動作
次の表で、日付に変換できる入力型を一覧にしています。
| 入力タイプ | 動作 | 
|---|---|
| Double | 切り捨てられた double 値で表されるミリ秒数に対応する日付を返します。 正の数は、1970 年 1 月 1 日からのミリ秒数に対応します。 負の数は、1970 年 1 月 1 日より前のミリ秒数に対応します。 | 
| 小数点 | 切り捨てられた小数値で表されるミリ秒数に対応する日付を返します。 正の数は、1970 年 1 月 1 日からのミリ秒数に対応します。 負の数は、1970 年 1 月 1 日より前のミリ秒数に対応します。 | 
| Long | long 値で表されるミリ秒数に対応する日付を返します。 正の数は、1970 年 1 月 1 日からのミリ秒数に対応します。 負の数は、1970 年 1 月 1 日より前のミリ秒数に対応します。 | 
| 文字列 | 日付文字列 に対応する日付を返します。 文字列は次のような有効な日付文字列でなければなりません。 
 | 
| ObjectId | ObjectId のタイムスタンプに対応する日付を返します。 | 
| タイムスタンプ | タイムスタンプに対応する日付を返します。 | 
次の表で、日付への変換例の一部を示します。
| 例 | 結果 | 
|---|---|
| 
 | ISODate("1973-10-20T21:20:00Z") | 
| 
 | ISODate("2009-09-19T14:53:56Z") | 
| 
 | ISODate("2004-11-19T11:33:20Z") | 
| 
 | ISODate("1935-02-22T12:26:40Z") | 
| 
 | ISODate( "2018-03-27T04:08:58Z") | 
| 
 | ISODate("2018-03-20T00:00:00Z") | 
| 
 | ISODate("2018-03-20T06:00:06Z") | 
| 
 | エラー | 
| 
 | 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 を使用します。