Skip to content

DOCS-6247 : Add examples for $sum returning 0 #2509

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
63 changes: 58 additions & 5 deletions source/reference/operator/aggregation/sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,38 @@ Definition
Behavior
--------

Non-numeric Values
~~~~~~~~~~~~~~~~~~
Non-Numeric or Non-Existent Fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If used on a field that contains both numeric and non-numeric values,
:group:`$sum` ignores the non-numeric values and returns the sum of the
numeric values.

If used on a field that does not exist in any document in the collection,
:group:`$sum` returns ``0`` for that field.

If all operands are non-numeric, :group:`$sum` returns ``0``.

.. list-table::
:header-rows: 1
:widths: 28 39 33

* - Example
- Field Values
- Results

* - ``{ $sum : <field> }``
- ``Numeric``
- ``Sum of Values``

* - ``{ $sum : <field> }``
- ``Numeric and Non-Numeric``
- ``Sum of Numeric Values``

* - ``{ $sum : <field> }``
- ``Non-Numeric or Non-Existent``
- ``0``

:group:`$sum` ignores non-numeric values. If all operands are
non-numeric, :group:`$sum` returns zero.

Array Operand
~~~~~~~~~~~~~
Expand Down Expand Up @@ -103,6 +130,32 @@ The operation returns the following results:
{ "_id" : { "day" : 34, "year" : 2014 }, "totalAmount" : 45, "count" : 2 }
{ "_id" : { "day" : 1, "year" : 2014 }, "totalAmount" : 20, "count" : 1 }

Using :group:`$sum` on a non-existent field returns a value of ``0``.
The following operation attempts to :group:`$sum` on ``qty``:

.. code-block:: javascript

db.sales.aggregate(
[
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
totalAmount: { $sum: "$qty" },
count: { $sum: 1 }
}
}
]
)

The operation returns:

.. code-block:: javascript

{ "_id" : { "day" : 46, "year" : 2014 }, "totalAmount" : 0, "count" : 2 }
{ "_id" : { "day" : 34, "year" : 2014 }, "totalAmount" : 0, "count" : 2 }
{ "_id" : { "day" : 1, "year" : 2014 }, "totalAmount" : 0, "count" : 1 }

Use in ``$project`` Stage
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -121,7 +174,7 @@ total lab scores, and the total of the final and the midterm:
.. code-block:: javascript

db.students.aggregate([
{
{
$project: {
quizTotal: { $sum: "$quizzes"},
labTotal: { $sum: "$labs" },
Expand Down