@@ -98,11 +98,11 @@ date:
98
98
99
99
The string must be a valid date string, such as:
100
100
101
- - "2018-03-03 "
101
+ - "2018-03-20 "
102
102
103
- - "2018-03-03T12 :00:00Z"
103
+ - "2018-03-20T12 :00:00Z"
104
104
105
- - "2018-03-03T12 :00:00+0500"
105
+ - "2018-03-20T12 :00:00+0500"
106
106
107
107
* - ObjectId
108
108
@@ -125,16 +125,16 @@ The following table lists some conversion to date examples:
125
125
- ISODate("2009-09-19T14:53:56Z")
126
126
127
127
* - ``{$toDate: NumberLong("1100000000000")}``
128
- - ISODate("2004-11-09T11 :33:20Z")
128
+ - ISODate("2004-11-19T11 :33:20Z")
129
129
130
130
* - ``{$toDate: NumberLong("-1100000000000")}``
131
131
- ISODate("1935-02-22T12:26:40Z")
132
132
133
133
* - ``{$toDate: ObjectId("5ab9c3da31c2ab715d421285")}``
134
134
- ISODate("2018-03-27T04:08:58Z")
135
135
136
- * - ``{$toDate: "2018-03-03 "}``
137
- - ISODate("2018-03-03T00 :00:00Z")
136
+ * - ``{$toDate: "2018-03-20 "}``
137
+ - ISODate("2018-03-20T00 :00:00Z")
138
138
139
139
* - ``{$toDate: "2018-03-20 11:00:06 +0500"}``
140
140
- ISODate("2018-03-20T06:00:06Z")
@@ -150,16 +150,15 @@ Create a collection ``orders`` with the following documents:
150
150
.. code-block:: javascript
151
151
152
152
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" }
157
157
] )
158
158
159
159
The following aggregation operation on the ``orders`` collection
160
160
converts the ``order_date`` to date before sorting by the date value:
161
161
162
-
163
162
.. code-block:: javascript
164
163
165
164
// 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:
179
178
db.orders.aggregate( [
180
179
dateConversionStage,
181
180
sortStage
182
- ])
181
+ ] )
183
182
184
183
The operation returns the following documents:
185
184
186
185
.. code-block:: javascript
187
186
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
+ }
192
219
193
220
.. note::
194
221
0 commit comments