Skip to content

Commit 48d724a

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-13304 toDate (#6006) (#6007)
Co-authored-by: jason-price-mongodb <jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com> Co-authored-by: jason-price-mongodb <jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com>
1 parent 8c99549 commit 48d724a

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
@@ -98,11 +98,11 @@ date:
9898

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

101-
- "2018-03-03"
101+
- "2018-03-20"
102102

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

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

107107
* - ObjectId
108108

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

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

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

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

136-
* - ``{$toDate: "2018-03-03"}``
137-
- ISODate("2018-03-03T00:00:00Z")
136+
* - ``{$toDate: "2018-03-20"}``
137+
- ISODate("2018-03-20T00:00:00Z")
138138

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

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

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

162-
163162
.. code-block:: javascript
164163

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

184183
The operation returns the following documents:
185184

186185
.. code-block:: javascript
187186

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

193220
.. note::
194221

0 commit comments

Comments
 (0)