$toDate (aggregation)

Definition

  • $toDate

New in version 4.0.

Converts a value to a date. If the value cannot be convertedto a date, $toDate errors. If the value is null ormissing, $toDate returns null.

$toDate has the following syntax:

  1. {
  2. $toDate: <expression>
  3. }

The $toDate takes any valid expression.

The $toDate is a shorthand for the following$convert expression:

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

See also

$convert and $dateFromString

Behavior

The following table lists the input types that can be converted to adate:

Input TypeBehavior
DoubleReturns a date that corresponds to the number of millisecondsrepresented by the truncated double value.Positive number corresponds to the number of milliseconds sinceJan 1, 1970.Negative number corresponds to the number of milliseconds beforeJan 1, 1970.
DecimalReturns a date that corresponds to the number of millisecondsrepresented by the truncated decimal value.Positive number corresponds to the number of milliseconds sinceJan 1, 1970.Negative number corresponds to the number of milliseconds beforeJan 1, 1970.
LongReturns a date that corresponds to the number of millisecondsrepresented by the long value.Positive number corresponds to the number of milliseconds sinceJan 1, 1970.Negative number corresponds to the number of milliseconds beforeJan 1, 1970.
StringReturns a date that corresponds to the date string.The string must be a valid date string, such as:- “2018-03-03”- “2018-03-03T12:00:00Z”- “2018-03-03T12:00:00+0500”
ObjectIdReturns a date that corresponds to the timestamp of theObjectId.

The following table lists some conversion to date examples:

ExampleResults
{$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

Example

Create a collection orders with the following documents:

  1. db.orders.insert( [
  2. { _id: 1, item: "apple", qty: 5, order_date: new Date("2018-03-10") },
  3. { _id: 2, item: "pie", qty: 10, order_date: new Date("2018-03-12")},
  4. { _id: 3, item: "ice cream", qty: 2, price: "4.99", order_date: "2018-03-05" },
  5. { _id: 4, item: "almonds" , qty: 5, price: 5, order_date: "2018-03-05 +10:00"}
  6. ] )

The following aggregation operation on the orders collectionconverts the order_date to date before sorting by the date value:

  1. // Define stage to add convertedDate field with the converted order_date value
  2.  
  3. dateConversionStage = {
  4. $addFields: {
  5. convertedDate: { $toDate: "$order_date" }
  6. }
  7. };
  8.  
  9. // Define stage to sort documents by the converted date
  10.  
  11. sortStage = {
  12. $sort: { "convertedDate": 1 }
  13. };
  14.  
  15. db.orders.aggregate( [
  16. dateConversionStage,
  17. sortStage
  18. ])

The operation returns the following documents:

  1. { "_id" : 4, "item" : "almonds", "qty" : 5, "price" : 5, "order_date" : "2018-03-05 +10:00", "convertedDate" : ISODate("2018-03-04T14:00:00Z") }
  2. { "_id" : 3, "item" : "ice cream", "qty" : 2, "price" : "4.99", "order_date" : "2018-03-05", "convertedDate" : ISODate("2018-03-05T00:00:00Z") }
  3. { "_id" : 1, "item" : "apple", "qty" : 5, "order_date" : ISODate("2018-03-10T00:00:00Z"), "convertedDate" : ISODate("2018-03-10T00:00:00Z") }
  4. { "_id" : 2, "item" : "pie", "qty" : 10, "order_date" : ISODate("2018-03-12T00:00:00Z"), "convertedDate" : ISODate("2018-03-12T00:00:00Z") }

Note

If the conversion operation encounters an error, the aggregationoperation stops and throws an error. To override this behavior, use$convert instead.