Skip to content
Closed
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
19 changes: 18 additions & 1 deletion source/reference/aggregation/cond.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,26 @@ $cond (aggregation)
.. code-block:: javascript

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

Takes an array with three expressions, where the first expression
evaluates to a Boolean value. If the first expression evaluates to true,
:expression:`$cond` returns the value of the second expression. If the
first expression evaluates to false, :expression:`$cond` evaluates and
returns the third expression.


.. example::

The following aggregation returns the number of documents where the
amount field is greater than 100.

.. code-block:: javascript

db.accounts.aggregate([
{
$group: {
_id: "totals",
totalAmount: { $sum: "$amount" },
maxAmount: { $max: "$amount" },
overMaxCount: { $sum: { $cond: [ { $gt: [ "$amount", 100 ] }, 1, 0 ] } },
}} ]);