Skip to content

DOCS-1152: Unclear how to use and aggregation operators #1295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions source/reference/aggregation/cond.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,42 @@ $cond (aggregation)

.. expression:: $cond

:expression:`$cond` is a ternary operator that takes an array
of three expressions, where the first expression evaluates to a
Boolean value. If the first expression evaluates to ``true``, then
:expression:`$cond` evaluates and returns the value of the second
expression. If the first expression evaluates to ``false``, then
:expression:`$cond` evaluates and returns the third expression.

Use the :expression:`$cond` operator with the following syntax:

.. code-block:: javascript

{ $cond: [ <boolean-expression>, <true-case>, <false-case> ] }

:expression:`$cond` is a ternary operator that takes an array of
three expressions, where the first expression evaluates to a Boolean
value. If the first evaluates to ``true``, :expression:`$cond`
evaluates and returns the value of the second expression. If the
first expression evaluates to ``false``, :expression:`$cond`
evaluates and returns the third expression.
All three values in the array specified to :expression:`$cond`
must be valid MongoDB :doc:`aggregation expressions
</reference/aggregation/operators>` or document fields. Do
not use JavaScript in any aggregation statements, including
:expression:`$cond`.

.. example::

The following aggregation on the ``survey`` collection groups
by the ``item_id`` field and returns a ``weightedCount``
for each ``item_id``. The :group:`$sum` operator uses the
:expression:`$cond` expression to add either ``2`` if the value
stored in the ``level`` field is ``E`` and ``1`` otherwise.

.. code-block:: javascript

db.survey.aggregate(
[
{
$group: {
_id: "$item_id",
weightedCount: { $sum: { $cond: [ { $eq: [ "$level", "E" ] } , 2, 1 ] } }
}
}
]
)
37 changes: 33 additions & 4 deletions source/reference/aggregation/ifNull.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,42 @@ $ifNull (aggregation)

.. expression:: $ifNull

Takes an array with two expressions. :expression:`$ifNull` returns
the first expression if it evaluates to a non-``null`` value.
Otherwise, :expression:`$ifNull` returns the second expression's
value.

Use the :expression:`$ifNull` operator with the following syntax:

.. code-block:: javascript

{ $ifNull: [ <expression>, <replacement-if-null> ] }

Takes an array with two expressions. :expression:`$ifNull` returns
the first expression if it evaluates to a non-null
value. Otherwise, :expression:`$ifNull` returns the second
expression’s value.
Both values in the array specified to :expression:`$ifNull`
must be valid MongoDB :doc:`aggregation expressions
</reference/aggregation/operators>` or document fields. Do
not use JavaScript in any aggregation statements, including
:expression:`$ifNull`.

.. example::

The following aggregation on the ``offSite`` collection
groups by the ``location`` field and returns a count for each
location. If the ``location`` field contains ``null``, the
:expression:`$ifNull` returns ``"Unspecified"`` as the value.
MongoDB assigns the returned value to ``_id`` in the aggregated
document.

.. code-block:: javascript

db.offSite.aggregate(
[
{
$group: {
_id: { $ifNull: [ "$location", "Unspecified" ] },
count: { $sum: 1 }
}
}
]
)