Skip to content

Commit 25b4e8e

Browse files
DOCS-14034-Extend-ifNull-to-accept-more-than-two-arguments
1 parent e347780 commit 25b4e8e

File tree

2 files changed

+102
-26
lines changed

2 files changed

+102
-26
lines changed

source/reference/operator/aggregation/ifNull.txt

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,68 @@ Definition
1515

1616
.. expression:: $ifNull
1717

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+
}
2445

25-
.. code-block:: javascript
46+
In MongoDB 4.4 and earlier versions, :expression:`$ifNull` only
47+
accepts a single input expression:
2648

27-
{ $ifNull: [ <expression>, <replacement-expression-if-null> ] }
49+
.. code-block:: none
50+
:copyable: false
2851

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+
}
3258

33-
Example
34-
-------
59+
Examples
60+
--------
3561

36-
The following example use a ``inventory`` collection with the following
37-
documents:
62+
This ``inventory`` collection is used in the examples:
3863

3964
.. code-block:: javascript
4065

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+
~~~~~~~~~~~~~~~~~~~~~~~
4474

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

5081
.. code-block:: javascript
5182

@@ -60,10 +91,48 @@ exist:
6091
]
6192
)
6293

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:
64132

65133
.. code-block:: javascript
134+
:copyable: false
66135

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" }

source/release-notes/5.0.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ New Aggregation Operators
4040
General Aggregation Improvements
4141
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4242

43+
Multiple Input Expressions for ``$ifNull`` Expression
44+
`````````````````````````````````````````````````````
45+
46+
Starting in MongoDB 5.0, you can specify multiple input expressions for
47+
the :expression:`$ifNull` expression before returning a replacement
48+
expression.
49+
4350
Aggregate ``let`` Option
4451
````````````````````````
4552

0 commit comments

Comments
 (0)