On this page本页内容
$toDate
¶New in version 4.0.版本4.0中的新功能。
Converts a value to a date. 将值转换为日期。If the value cannot be converted to a date, 如果该值无法转换为日期,$toDate
errors. $toDate
错误。If the value is null or missing, 如果值为$toDate
returns null.null
或缺失,$toDate
返回null
。
$toDate
has the following syntax:语法如下所示:
The $toDate
takes any valid expression.$toDate
接受任何有效表达式。
The $toDate
is a shorthand for the following $convert
expression:$toDate
是以下$convert
表达式的简写形式:
See also参阅
The following table lists the input types that can be converted to a date:下表列出了可以转换为日期的输入类型:
Input Type | Behavior |
---|---|
Double |
|
Decimal |
|
Long |
|
String |
|
ObjectId |
The following table lists some conversion to date examples:下表列出了一些到日期的转换示例:
{$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-09T11:33:20Z”) |
{$toDate: NumberLong("-1100000000000")} |
ISODate(“1935-02-22T12:26:40Z”) |
{$toDate: ObjectId("5ab9c3da31c2ab715d421285")} |
ISODate(“2018-03-27T04:08:58Z”) |
{$toDate: "2018-03-03"} |
ISODate(“2018-03-03T00:00:00Z”) |
{$toDate: "2018-03-20 11:00:06 +0500"} |
ISODate(“2018-03-20T06:00:06Z”) |
{$toDate: "Friday"} |
Error |
Create a collection 使用以下文档创建集合orders
with the following documents:orders
:
The following aggregation operation on the orders
collection converts the order_date
to date before sorting by the date value:orders
集合上的以下聚合操作将order_date
转换为按日期值排序之前的日期:
The operation returns the following documents:该操作返回以下文档: