Skip to content

DOCS-1369 map function need not necessarily emit #1015

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
42 changes: 36 additions & 6 deletions source/includes/parameters-map-reduce.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
A JavaScript function that associates or "maps" a ``value`` with a
``key`` and emits the ``key`` and value ``pair``.

The ``map`` function processes every input document for the map-reduce
operation. The map-reduce operation groups the emitted ``value``
objects by the ``key`` and passes these groupings to the ``reduce``
function.
The ``map`` function processes every input document for the
map-reduce operation. However, the ``map`` function can call emit
any number of times, including 0, for each input document. The
map-reduce operation groups the emitted ``value`` objects by the
``key`` and passes these groupings to the ``reduce`` function.
See below for requirements for the ``map`` function.

:param reduce:

Expand All @@ -16,6 +18,8 @@
The ``reduce`` function accepts two arguments: ``key`` and
``values``. The ``values`` argument is an array whose elements are
the ``value`` objects that are "mapped" to the ``key``.
See below for requirements for the ``reduce`` function.


:param out:

Expand Down Expand Up @@ -128,8 +132,29 @@ The ``map`` function exhibits the following behaviors:
- A single emit can only hold half of MongoDB's :ref:`maximum BSON
document size <limit-bson-document-size>`.

- There is no limit to the number of times you may call the ``emit``
function per document.
- The ``map`` function can call ``emit(key,value)`` any number of
times, including 0, per each input document.

The following ``map`` function may call ``emit(key,value)`` either
0 or 1 times depending on the value of the input document's
``status`` field:

.. code-block:: javascript

function() {
if (this.status == 'A')
emit(this.cust_id, 1);
}

The following ``map`` function may call ``emit(key,value)``
multiple times depending on the number of elements in the input
document's ``items`` field:

.. code-block:: javascript

function() {
this.items.forEach(function(item){ emit(item.sku, 1); });
}

- The ``map`` function can access the variables defined in the
``scope`` parameter.
Expand Down Expand Up @@ -157,6 +182,11 @@ The ``reduce`` function exhibits the following behaviors:
- MongoDB will **not** call the ``reduce`` function for a key
that has only a single value.

- MongoDB can invoke the ``reduce`` function more than once for the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is s/can/may/ acceptable?

same key. In this case, the previous output from the ``reduce``
function for that key will become one of the input values to the next
``reduce`` function invocation for that key.

- The ``reduce`` function can access the variables defined
in the ``scope`` parameter.

Expand Down