Skip to content

Added an example of how to use cond #1073

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
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 ] } },
}} ]);