Skip to content

Commit 3d50145

Browse files
committed
DOCS-11442: Add "onError" and "onNull" options to $dateFromString
1 parent f8c374d commit 3d50145

File tree

1 file changed

+135
-34
lines changed

1 file changed

+135
-34
lines changed

source/reference/operator/aggregation/dateFromString.txt

Lines changed: 135 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ Definition
2323

2424
.. code-block:: javascript
2525

26-
{ $dateFromString: {
27-
dateString: <dateStringExpression>,
26+
{ $dateFromString: {
27+
dateString: <dateStringExpression>,
2828
format: <formatStringExpression>,
29-
timezone: <tzExpression>
29+
timezone: <tzExpression>,
30+
onError: <onErrorExpression>,
31+
onNull: <onNullExpression>
3032
} }
3133

3234
The :pipeline:`$dateFromString` takes a document with the following fields:
@@ -40,13 +42,13 @@ Definition
4042

4143
* - ``dateString``
4244

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.
4648

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
5052
time zone information in the ``dateString``.
5153

5254
* - ``format``
@@ -65,19 +67,19 @@ Definition
6567

6668
* - ``timezone``
6769

68-
- Optional. The time zone to use to format the date.
70+
- Optional. The time zone to use to format the date.
6971

7072
.. note::
7173

72-
If the ``dateString`` argument is formatted like
74+
If the ``dateString`` argument is formatted like
7375
'2017-02-08T12:10:40.787Z', in which the 'Z' at the end indicates Zulu
7476
time (UTC time zone), you cannot specify the ``timezone`` argument.
7577

76-
``<timezone>`` allows for the following options and expressions
78+
``<timezone>`` allows for the following options and expressions
7779
that evaluate to them:
7880

7981
- 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>`_,
8183
such as ``"Europe/London"`` or ``"America/New_York"``, or
8284

8385
- a UTC offset in the form:
@@ -93,8 +95,26 @@ Definition
9395
For more information on expressions, see
9496
:ref:`aggregation-expressions`.
9597

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``.
98118

99119
.. seealso:: :expression:`$toDate` and :expression:`$convert`
100120

@@ -107,21 +127,21 @@ Behavior
107127

108128
* - Example
109129
- Results
110-
130+
111131
* - .. code-block:: javascript
112132
:copyable: false
113133

114-
115-
{ $dateFromString: {
116-
dateString: "2017-02-08T12:10:40.787"
134+
135+
{ $dateFromString: {
136+
dateString: "2017-02-08T12:10:40.787"
117137
} }
118138

119139
- ``ISODate("2017-02-08T12:10:40.787Z")``
120140

121141
* - .. code-block:: javascript
122142
:copyable: false
123143

124-
{ $dateFromString: {
144+
{ $dateFromString: {
125145
dateString: "2017-02-08T12:10:40.787",
126146
timezone: "America/New_York"
127147
} }
@@ -131,7 +151,7 @@ Behavior
131151
* - .. code-block:: javascript
132152
:copyable: false
133153

134-
{ $dateFromString: {
154+
{ $dateFromString: {
135155
dateString: "2017-02-08"
136156
} }
137157

@@ -140,26 +160,35 @@ Behavior
140160
* - .. code-block:: javascript
141161
:copyable: false
142162

143-
{ $dateFromString: {
163+
{ $dateFromString: {
144164
dateString: "06-15-2018",
145165
format: "%m-%d-%Y"
146166
} }
147167

148168
- ``ISODate("2018-06-15T00:00:00Z")``
149-
169+
150170
* - .. code-block:: javascript
151171
:copyable: false
152172

153-
{ $dateFromString: {
173+
{ $dateFromString: {
154174
dateString: "15-06-2018",
155175
format: "%d-%m-%Y"
156176
} }
157177

158178
- ``ISODate("2018-06-15T00:00:00Z")``
159179

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+
--------
162189

190+
Converting Dates
191+
~~~~~~~~~~~~~~~~
163192

164193
Consider a collection ``logmessages`` that contains the following
165194
documents with dates.
@@ -171,7 +200,7 @@ documents with dates.
171200
{ _id: 3, message: " Step 1: Ended " },
172201
{ _id: 4, date: "2017-02-09", timezone: "Europe/London", message: "Step 2: Started"}
173202
{ _id: 5, date: "2017-02-09T03:35:02.055", timezone: "+0530", message: "Step 2: In Progress"}
174-
203+
175204
The following aggregation uses $dateFromString to convert the ``date`` value
176205
to a date object:
177206

@@ -192,14 +221,14 @@ The above aggregation returns the following documents and converts each ``date``
192221
to the Eastern Time Zone:
193222

194223
.. code-block:: javascript
195-
224+
196225
{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
197226
{ "_id" : 2, "date" : ISODate("2017-02-08T05:00:00Z") }
198227
{ "_id" : 3, "date" : null }
199228
{ "_id" : 4, "date" : ISODate("2017-02-09T05:00:00Z") }
200229
{ "_id" : 5, "date" : ISODate("2017-02-09T08:35:02.055Z") }
201230

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
203232
hard coded argument. For example:
204233

205234
.. code-block:: javascript
@@ -216,7 +245,7 @@ hard coded argument. For example:
216245
} ] )
217246

218247
The above aggregation returns the following documents and converts each ``date`` field
219-
to their respective UTC representations.
248+
to their respective UTC representations.
220249

221250
.. code-block:: javascript
222251

@@ -226,10 +255,82 @@ to their respective UTC representations.
226255
{ "_id" : 4, "date" : ISODate("2017-02-09T00:00:00Z") }
227256
{ "_id" : 5, "date" : ISODate("2017-02-08T22:05:02.055Z") }
228257

258+
``onError``
259+
~~~~~~~~~~~
229260

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.
231265

232-
Format Specifiers
233-
-----------------
266+
For example, given a collection ``dates`` with the following
267+
documents:
234268

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

Comments
 (0)