@@ -23,10 +23,12 @@ Definition
23
23
24
24
.. code-block:: javascript
25
25
26
- { $dateFromString: {
27
- dateString: <dateStringExpression>,
26
+ { $dateFromString: {
27
+ dateString: <dateStringExpression>,
28
28
format: <formatStringExpression>,
29
- timezone: <tzExpression>
29
+ timezone: <tzExpression>,
30
+ onError: <onErrorExpression>,
31
+ onNull: <onNullExpression>
30
32
} }
31
33
32
34
The :pipeline:`$dateFromString` takes a document with the following fields:
@@ -40,13 +42,13 @@ Definition
40
42
41
43
* - ``dateString``
42
44
43
- - The date/time string to convert to a date object. See
44
- :doc:`Date </reference/method/Date>` for
45
- more information on date/time formats.
45
+ - The date/time string to convert to a date object. See
46
+ :doc:`Date </reference/method/Date>` for
47
+ more information on date/time formats.
46
48
47
- .. note::
48
-
49
- If specifying the ``timezone`` option to the operator, do not include
49
+ .. note::
50
+
51
+ If specifying the ``timezone`` option to the operator, do not include
50
52
time zone information in the ``dateString``.
51
53
52
54
* - ``format``
@@ -65,19 +67,19 @@ Definition
65
67
66
68
* - ``timezone``
67
69
68
- - Optional. The time zone to use to format the date.
70
+ - Optional. The time zone to use to format the date.
69
71
70
72
.. note::
71
73
72
- If the ``dateString`` argument is formatted like
74
+ If the ``dateString`` argument is formatted like
73
75
'2017-02-08T12:10:40.787Z', in which the 'Z' at the end indicates Zulu
74
76
time (UTC time zone), you cannot specify the ``timezone`` argument.
75
77
76
- ``<timezone>`` allows for the following options and expressions
78
+ ``<timezone>`` allows for the following options and expressions
77
79
that evaluate to them:
78
80
79
81
- an `Olson Timezone Identifier
80
- <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones>`_,
82
+ <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones>`_,
81
83
such as ``"Europe/London"`` or ``"America/New_York"``, or
82
84
83
85
- a UTC offset in the form:
@@ -93,8 +95,26 @@ Definition
93
95
For more information on expressions, see
94
96
:ref:`aggregation-expressions`.
95
97
96
- If the ``dateStringExpression`` or ``tzExpression`` evaluate to null, missing or
97
- undefined, the result of the $dateFromString expression is null.
98
+ * - ``onError``
99
+
100
+ - Optional. If :expression:`$dateFromString` encounters an error while
101
+ parsing the given ``dateString``, it outputs the result value
102
+ of the provided ``onError`` :ref:`expression <aggregation-expressions>`.
103
+ This result value can be of any type.
104
+
105
+ If you do not specify ``onError``, :expression:`$dateFromString`
106
+ throws an error if it cannot parse ``dateString``.
107
+
108
+ * - ``onNull``
109
+
110
+ - Optional. If the ``dateString`` provided to
111
+ :expression:`$dateFromString` is ``null`` or missing, it outputs
112
+ the result value of the provided ``onNull``
113
+ :ref:`expression <aggregation-expressions>`.
114
+ This result value can be of any type.
115
+
116
+ If you do not specify ``onNull`` and ``dateString`` is ``null``
117
+ or missing, then :expression:`$dateFromString` outputs ``null``.
98
118
99
119
.. seealso:: :expression:`$toDate` and :expression:`$convert`
100
120
@@ -107,21 +127,21 @@ Behavior
107
127
108
128
* - Example
109
129
- Results
110
-
130
+
111
131
* - .. code-block:: javascript
112
132
:copyable: false
113
133
114
-
115
- { $dateFromString: {
116
- dateString: "2017-02-08T12:10:40.787"
134
+
135
+ { $dateFromString: {
136
+ dateString: "2017-02-08T12:10:40.787"
117
137
} }
118
138
119
139
- ``ISODate("2017-02-08T12:10:40.787Z")``
120
140
121
141
* - .. code-block:: javascript
122
142
:copyable: false
123
143
124
- { $dateFromString: {
144
+ { $dateFromString: {
125
145
dateString: "2017-02-08T12:10:40.787",
126
146
timezone: "America/New_York"
127
147
} }
@@ -131,7 +151,7 @@ Behavior
131
151
* - .. code-block:: javascript
132
152
:copyable: false
133
153
134
- { $dateFromString: {
154
+ { $dateFromString: {
135
155
dateString: "2017-02-08"
136
156
} }
137
157
@@ -140,26 +160,35 @@ Behavior
140
160
* - .. code-block:: javascript
141
161
:copyable: false
142
162
143
- { $dateFromString: {
163
+ { $dateFromString: {
144
164
dateString: "06-15-2018",
145
165
format: "%m-%d-%Y"
146
166
} }
147
167
148
168
- ``ISODate("2018-06-15T00:00:00Z")``
149
-
169
+
150
170
* - .. code-block:: javascript
151
171
:copyable: false
152
172
153
- { $dateFromString: {
173
+ { $dateFromString: {
154
174
dateString: "15-06-2018",
155
175
format: "%d-%m-%Y"
156
176
} }
157
177
158
178
- ``ISODate("2018-06-15T00:00:00Z")``
159
179
160
- Example
161
- -------
180
+ .. _dateFromString-format-specifiers:
181
+
182
+ Format Specifiers
183
+ -----------------
184
+
185
+ .. include:: /includes/extracts/date-format-specifiers-dateFromString.rst
186
+
187
+ Examples
188
+ --------
162
189
190
+ Converting Dates
191
+ ~~~~~~~~~~~~~~~~
163
192
164
193
Consider a collection ``logmessages`` that contains the following
165
194
documents with dates.
@@ -171,7 +200,7 @@ documents with dates.
171
200
{ _id: 3, message: " Step 1: Ended " },
172
201
{ _id: 4, date: "2017-02-09", timezone: "Europe/London", message: "Step 2: Started"}
173
202
{ _id: 5, date: "2017-02-09T03:35:02.055", timezone: "+0530", message: "Step 2: In Progress"}
174
-
203
+
175
204
The following aggregation uses $dateFromString to convert the ``date`` value
176
205
to a date object:
177
206
@@ -192,14 +221,14 @@ The above aggregation returns the following documents and converts each ``date``
192
221
to the Eastern Time Zone:
193
222
194
223
.. code-block:: javascript
195
-
224
+
196
225
{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
197
226
{ "_id" : 2, "date" : ISODate("2017-02-08T05:00:00Z") }
198
227
{ "_id" : 3, "date" : null }
199
228
{ "_id" : 4, "date" : ISODate("2017-02-09T05:00:00Z") }
200
229
{ "_id" : 5, "date" : ISODate("2017-02-09T08:35:02.055Z") }
201
230
202
- The ``timezone`` argument can also be provided through a document field instead of a
231
+ The ``timezone`` argument can also be provided through a document field instead of a
203
232
hard coded argument. For example:
204
233
205
234
.. code-block:: javascript
@@ -216,7 +245,7 @@ hard coded argument. For example:
216
245
} ] )
217
246
218
247
The above aggregation returns the following documents and converts each ``date`` field
219
- to their respective UTC representations.
248
+ to their respective UTC representations.
220
249
221
250
.. code-block:: javascript
222
251
@@ -226,10 +255,82 @@ to their respective UTC representations.
226
255
{ "_id" : 4, "date" : ISODate("2017-02-09T00:00:00Z") }
227
256
{ "_id" : 5, "date" : ISODate("2017-02-08T22:05:02.055Z") }
228
257
258
+ ``onError``
259
+ ~~~~~~~~~~~
229
260
230
- .. _dateFromString-format-specifiers:
261
+ If your collection contains documents with unparsable date strings,
262
+ :expression:`$dateFromString` throws an error unless you provide an
263
+ :ref:`aggregation expression <aggregation-expressions>` to the optional
264
+ ``onError`` parameter.
231
265
232
- Format Specifiers
233
- -----------------
266
+ For example, given a collection ``dates`` with the following
267
+ documents:
234
268
235
- .. include:: /includes/extracts/date-format-specifiers-dateFromString.rst
269
+ .. code-block:: javascript
270
+
271
+ { "_id" : 1, "date" : "2017-02-08T12:10:40.787", timezone: "America/New_York" },
272
+ { "_id" : 2, "date" : "20177-02-09T03:35:02.055", timezone: "America/New_York" }
273
+
274
+ You can use the ``onError`` parameter to return the invalid date in
275
+ its original string form:
276
+
277
+ .. code-block:: javascript
278
+
279
+ db.dates.aggregate( [ {
280
+ $project: {
281
+ date: {
282
+ $dateFromString: {
283
+ dateString: '$date',
284
+ timezone: '$timezone',
285
+ onError: '$date'
286
+ }
287
+ }
288
+ }
289
+ } ] )
290
+
291
+ This returns the following documents:
292
+
293
+ .. code-block:: javascript
294
+
295
+ { "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
296
+ { "_id" : 2, "date" : "20177-02-09T03:35:02.055" }
297
+
298
+ ``onNull``
299
+ ~~~~~~~~~~~
300
+
301
+ If your collection contains documents with ``null`` date strings,
302
+ :expression:`$dateFromString` returns ``null`` unless you provide an
303
+ :ref:`aggregation expression <aggregation-expressions>` to the optional
304
+ ``onNull`` parameter.
305
+
306
+ For example, given a collection ``dates`` with the following
307
+ documents:
308
+
309
+ .. code-block:: javascript
310
+
311
+ { "_id" : 1, "date" : "2017-02-08T12:10:40.787", timezone: "America/New_York" },
312
+ { "_id" : 2, "date" : null, timezone: "America/New_York" }
313
+
314
+ You can use the ``onNull`` parameter to have :expression:`$dateFromString`
315
+ return a date representing the :term:`unix epoch` instead of ``null``:
316
+
317
+ .. code-block:: javascript
318
+
319
+ db.dates.aggregate( [ {
320
+ $project: {
321
+ date: {
322
+ $dateFromString: {
323
+ dateString: '$date',
324
+ timezone: '$timezone',
325
+ onNull: new Date(0)
326
+ }
327
+ }
328
+ }
329
+ } ] )
330
+
331
+ This returns the following documents:
332
+
333
+ .. code-block:: javascript
334
+
335
+ { "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
336
+ { "_id" : 2, "date" : ISODate("1970-01-01T00:00:00Z") }
0 commit comments