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