Database Manual / Reference / Query Language / Expressions

$toDate (expression operator)(表达式运算符)

Definition定义

$toDate

Converts a value to a date. If the value cannot be converted to a date, $toDate errors. If the value is null or missing, $toDate returns null.将值转换为日期。如果该值无法转换为日期,则$toDate错误。如果该值为null或缺失,$toDate将返回null

$toDate has the following syntax:具有以下语法:

{
$toDate: <expression>
}

The $toDate takes any valid expression.$toDate接受任何有效表达式。

The $toDate is a shorthand for the following $convert expression:$toDate是以下$convert表达式的简写:

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

Behavior行为

The following table lists the input types that can be converted to a date:下表列出了可以转换为日期的输入类型:

Input Type输入类型Behavior行为
Double双精度浮点数

Returns a date that corresponds to the number of milliseconds represented by the truncated double value.返回一个日期,该日期对应于截断的双精度值所表示的毫秒数。

Positive number corresponds to the number of milliseconds since Jan 1, 1970.正数对应自1970年1月1日以来的毫秒数。

Negative number corresponds to the number of milliseconds before Jan 1, 1970.负数对应于1970年1月1日之前的毫秒数。

Decimal

Returns a date that corresponds to the number of milliseconds represented by the truncated decimal value.返回与截断的十进制值表示的毫秒数对应的日期。

Positive number corresponds to the number of milliseconds since Jan 1, 1970.正数对应自1970年1月1日以来的毫秒数。

Negative number corresponds to the number of milliseconds before Jan 1, 1970.负数对应于1970年1月1日之前的毫秒数。

Long长整数

Returns a date that corresponds to the number of milliseconds represented by the long value.返回与long值表示的毫秒数对应的日期。

Positive number corresponds to the number of milliseconds since Jan 1, 1970.正数对应自1970年1月1日以来的毫秒数。

Negative number corresponds to the number of milliseconds before Jan 1, 1970.负数对应于1970年1月1日之前的毫秒数。

String字符串

Returns a date that corresponds to the date string.返回与日期字符串对应的日期。

The string must be a valid date string, such as:字符串必须是有效的日期字符串,例如:

  • "2018-03-20"
  • "2018-03-20T12:00:00Z"
  • "2018-03-20T12:00:00+0500"
ObjectIdReturns a date that corresponds to the timestamp of the ObjectId.返回与ObjectId的时间戳对应的日期。
TimestampReturns a date that corresponds to the timestamp.返回与时间戳对应的日期。

The following table lists some conversion to date examples:下表列出了一些迄今为止的转换示例:

Example示例Results结果
{$toDate: 120000000000.5}ISODate("1973-10-20T21:20:00Z")
{$toDate: Decimal128("1253372036000.50")}ISODate("2009-09-19T14:53:56Z")
{$toDate: Long("1100000000000")}ISODate("2004-11-19T11:33:20Z")
{$toDate: Long("-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"}Error
{$toDate: Timestamp({ t: 1637688118, i: 1 })}ISODate("2021-11-23T17:21:58.00Z")

Example示例

Create a collection orders with the following documents:使用以下文档创建集合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" }
] )

The following aggregation operation on the orders collection converts the order_date to date before sorting by the date value:以下对orders集合的聚合操作将order_date转换为日期,然后再按日期值排序:

// Define stage to add convertedDate field with the converted order_date value定义阶段以添加具有转换后的order_date值的convertedDate字段

dateConversionStage = {
$addFields: {
convertedDate: { $toDate: "$order_date" }
}
};

// Define stage to sort documents by the converted date定义按转换日期对文档进行排序的阶段

sortStage = {
$sort: { "convertedDate": 1 }
};

db.orders.aggregate( [
dateConversionStage,
sortStage
] )

The operation returns the following documents:该操作返回以下文档:

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

Note

If the conversion operation encounters an error, the aggregation operation stops and throws an error. To override this behavior, use $convert instead.如果转换操作遇到错误,聚合操作将停止并抛出错误。要覆盖此行为,请改用$converter