Skip to content

DOCSP-13304 toDate #6006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions source/reference/operator/aggregation/toDate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ date:

The string must be a valid date string, such as:

- "2018-03-03"
- "2018-03-20"

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

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

* - ObjectId

Expand All @@ -125,16 +125,16 @@ The following table lists some conversion to date examples:
- ISODate("2009-09-19T14:53:56Z")

* - ``{$toDate: NumberLong("1100000000000")}``
- ISODate("2004-11-09T11:33:20Z")
- 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-03"}``
- ISODate("2018-03-03T00:00:00Z")
* - ``{$toDate: "2018-03-20"}``
- ISODate("2018-03-20T00:00:00Z")

* - ``{$toDate: "2018-03-20 11:00:06 +0500"}``
- ISODate("2018-03-20T06:00:06Z")
Expand All @@ -150,16 +150,15 @@ Create a collection ``orders`` with the following documents:
.. code-block:: javascript

db.orders.insert( [
{ _id: 1, item: "apple", qty: 5, order_date: new Date("2018-03-10") },
{ _id: 2, item: "pie", qty: 10, order_date: new Date("2018-03-12")},
{ _id: 3, item: "ice cream", qty: 2, price: "4.99", order_date: "2018-03-05" },
{ _id: 4, item: "almonds" , qty: 5, price: 5, order_date: "2018-03-05 +10:00"}
{ _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:


.. code-block:: javascript

// Define stage to add convertedDate field with the converted order_date value
Expand All @@ -179,16 +178,44 @@ converts the ``order_date`` to date before sorting by the date value:
db.orders.aggregate( [
dateConversionStage,
sortStage
])
] )

The operation returns the following documents:

.. code-block:: javascript

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

Expand Down