@@ -15,37 +15,68 @@ Definition
15
15
16
16
.. expression:: $ifNull
17
17
18
- Evaluates an expression and returns the value of the expression if
19
- the expression evaluates to a non-null value. If the expression
20
- evaluates to a null value, including instances of undefined values
21
- or missing fields, returns the value of the replacement expression.
22
-
23
- The :expression:`$ifNull` expression has the following syntax:
18
+ .. versionchanged:: 5.0
19
+
20
+ The :expression:`$ifNull` expression evaluates input expressions for
21
+ null values and returns:
22
+
23
+ - The first non-null input :ref:`expression <aggregation-expressions>`
24
+ value found.
25
+
26
+ - A replacement :ref:`expression <aggregation-expressions>` value if all
27
+ input expressions evaluate to null.
28
+
29
+ :expression:`$ifNull` treats undefined values and missing fields as
30
+ null.
31
+
32
+ Syntax:
33
+
34
+ .. code-block:: none
35
+ :copyable: false
36
+
37
+ {
38
+ $ifNull: [
39
+ <input-expression-1>,
40
+ ...
41
+ <input-expression-n>,
42
+ <replacement-expression-if-null>
43
+ ]
44
+ }
24
45
25
- .. code-block:: javascript
46
+ In MongoDB 4.4 and earlier versions, :expression:`$ifNull` only
47
+ accepts a single input expression:
26
48
27
- { $ifNull: [ <expression>, <replacement-expression-if-null> ] }
49
+ .. code-block:: none
50
+ :copyable: false
28
51
29
- The arguments can be any valid :ref:`expression
30
- <aggregation-expressions>`. For more information on expressions, see
31
- :ref:`aggregation-expressions`.
52
+ {
53
+ $ifNull: [
54
+ <input-expression>,
55
+ <replacement-expression-if-null>
56
+ ]
57
+ }
32
58
33
- Example
34
- -------
59
+ Examples
60
+ --------
35
61
36
- The following example use a ``inventory`` collection with the following
37
- documents:
62
+ This ``inventory`` collection is used in the examples:
38
63
39
64
.. code-block:: javascript
40
65
41
- { "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }
42
- { "_id" : 2, "item" : "abc2", description: null, qty: 200 }
43
- { "_id" : 3, "item" : "xyz1", qty: 250 }
66
+ db.inventory.insertMany( [
67
+ { "_id" : 1, "item" : "buggy", description: "toy car", "quantity" : 300 },
68
+ { "_id" : 2, "item" : "bicycle", description: null, "quantity" : 200 },
69
+ { "_id" : 3, "item" : "flag" }
70
+ ] )
71
+
72
+ Single Input Expression
73
+ ~~~~~~~~~~~~~~~~~~~~~~~
44
74
45
- The following operation uses the :expression:`$ifNull` expression to
46
- return either the non-null ``description`` field value or the string
47
- ``"Unspecified"`` if the ``description`` field is null or does not
48
- exist:
75
+ The following example uses :expression:`$ifNull` to return:
76
+
77
+ - ``description`` if it is non-null.
78
+
79
+ - ``"Unspecified"`` string if ``description`` is null or missing.
49
80
50
81
.. code-block:: javascript
51
82
@@ -60,10 +91,48 @@ exist:
60
91
]
61
92
)
62
93
63
- The operation returns the following results:
94
+ Output:
95
+
96
+ .. code-block:: javascript
97
+ :copyable: false
98
+
99
+ { "_id" : 1, "item" : "buggy", "description" : "toy car" }
100
+ { "_id" : 2, "item" : "bicycle", "description" : "Unspecified" }
101
+ { "_id" : 3, "item" : "flag", "description" : "Unspecified" }
102
+
103
+ Multiple Input Expressions
104
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
105
+
106
+ .. versionadded:: 5.0
107
+
108
+ The following example uses :expression:`$ifNull` to return:
109
+
110
+ - ``description`` if it is non-null.
111
+
112
+ - ``quantity`` if ``description`` is null or missing and ``quantity``
113
+ is non-null.
114
+
115
+ - ``"Unspecified"`` string if ``description`` and ``quantity`` are both
116
+ null or missing.
117
+
118
+ .. code-block:: javascript
119
+
120
+ db.inventory.aggregate(
121
+ [
122
+ {
123
+ $project: {
124
+ item: 1,
125
+ value: { $ifNull: [ "$description", "$quantity", "Unspecified" ] }
126
+ }
127
+ }
128
+ ]
129
+ )
130
+
131
+ Output:
64
132
65
133
.. code-block:: javascript
134
+ :copyable: false
66
135
67
- { "_id" : 1, "item" : "abc1 ", "description " : "product 1 " }
68
- { "_id" : 2, "item" : "abc2 ", "description " : "Unspecified" }
69
- { "_id" : 3, "item" : "xyz1 ", "description " : "Unspecified" }
136
+ { "_id" : 1, "item" : "buggy ", "value " : "toy car " }
137
+ { "_id" : 2, "item" : "bicycle ", "value " : 200 }
138
+ { "_id" : 3, "item" : "flag ", "value " : "Unspecified" }
0 commit comments