Skip to content

Commit a73532a

Browse files
jason-price-mongodbjason-price-mongodb
andcommitted
DOCSP-13304 toDate (mongodb#6006)
Co-authored-by: jason-price-mongodb <jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com>
1 parent c56bc5c commit a73532a

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

source/reference/operator/aggregation/toDate.txt

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ date:
9696

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

99-
- "2018-03-03"
99+
- "2018-03-20"
100100

101-
- "2018-03-03T12:00:00Z"
101+
- "2018-03-20T12:00:00Z"
102102

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

105105
* - ObjectId
106106

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

125125
* - ``{$toDate: NumberLong("1100000000000")}``
126-
- ISODate("2004-11-09T11:33:20Z")
126+
- ISODate("2004-11-19T11:33:20Z")
127127

128128
* - ``{$toDate: NumberLong("-1100000000000")}``
129129
- ISODate("1935-02-22T12:26:40Z")
130130

131131
* - ``{$toDate: ObjectId("5ab9c3da31c2ab715d421285")}``
132132
- ISODate("2018-03-27T04:08:58Z")
133133

134-
* - ``{$toDate: "2018-03-03"}``
135-
- ISODate("2018-03-03T00:00:00Z")
134+
* - ``{$toDate: "2018-03-20"}``
135+
- ISODate("2018-03-20T00:00:00Z")
136136

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

150150
db.orders.insert( [
151-
{ _id: 1, item: "apple", qty: 5, order_date: new Date("2018-03-10") },
152-
{ _id: 2, item: "pie", qty: 10, order_date: new Date("2018-03-12")},
153-
{ _id: 3, item: "ice cream", qty: 2, price: "4.99", order_date: "2018-03-05" },
154-
{ _id: 4, item: "almonds" , qty: 5, price: 5, order_date: "2018-03-05 +10:00"}
151+
{ _id: 1, item: "apple", qty: 5, price: 2, order_date: new Date( "2018-03-20" ) },
152+
{ _id: 2, item: "pie", qty: 10, price: 3, order_date: new Date( "2018-03-22" ) },
153+
{ _id: 3, item: "ice cream", qty: 2, price: 4, order_date: "2018-03-15" },
154+
{ _id: 4, item: "almonds" , qty: 5, price: 7, order_date: "2018-03-15 +10:00" }
155155
] )
156156

157157
The following aggregation operation on the ``orders`` collection
158158
converts the ``order_date`` to date before sorting by the date value:
159159

160-
161160
.. code-block:: javascript
162161

163162
// Define stage to add convertedDate field with the converted order_date value
@@ -177,16 +176,44 @@ converts the ``order_date`` to date before sorting by the date value:
177176
db.orders.aggregate( [
178177
dateConversionStage,
179178
sortStage
180-
])
179+
] )
181180

182181
The operation returns the following documents:
183182

184183
.. code-block:: javascript
185184

186-
{ "_id" : 4, "item" : "almonds", "qty" : 5, "price" : 5, "order_date" : "2018-03-05 +10:00", "convertedDate" : ISODate("2018-03-04T14:00:00Z") }
187-
{ "_id" : 3, "item" : "ice cream", "qty" : 2, "price" : "4.99", "order_date" : "2018-03-05", "convertedDate" : ISODate("2018-03-05T00:00:00Z") }
188-
{ "_id" : 1, "item" : "apple", "qty" : 5, "order_date" : ISODate("2018-03-10T00:00:00Z"), "convertedDate" : ISODate("2018-03-10T00:00:00Z") }
189-
{ "_id" : 2, "item" : "pie", "qty" : 10, "order_date" : ISODate("2018-03-12T00:00:00Z"), "convertedDate" : ISODate("2018-03-12T00:00:00Z") }
185+
{
186+
_id: 4,
187+
item: 'almonds',
188+
qty: 5,
189+
price: 7,
190+
order_date: '2018-03-15 +10:00',
191+
convertedDate: ISODate("2018-03-14T14:00:00.000Z")
192+
},
193+
{
194+
_id: 3,
195+
item: 'ice cream',
196+
qty: 2,
197+
price: 4,
198+
order_date: '2018-03-15',
199+
convertedDate: ISODate("2018-03-15T00:00:00.000Z")
200+
},
201+
{
202+
_id: 1,
203+
item: 'apple',
204+
qty: 5,
205+
price: 2,
206+
order_date: ISODate("2018-03-20T00:00:00.000Z"),
207+
convertedDate: ISODate("2018-03-20T00:00:00.000Z")
208+
},
209+
{
210+
_id: 2,
211+
item: 'pie',
212+
qty: 10,
213+
price: 3,
214+
order_date: ISODate("2018-03-22T00:00:00.000Z"),
215+
convertedDate: ISODate("2018-03-22T00:00:00.000Z")
216+
}
190217

191218
.. note::
192219

0 commit comments

Comments
 (0)