Skip to content

Commit bee32ea

Browse files
p-mongop
andauthored
RUBY-2688 Document Collection#map_reduce (#2278)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 91102cf commit bee32ea

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

source/reference/aggregation.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _aggregation:
2+
13
***********
24
Aggregation
35
***********
@@ -7,7 +9,7 @@ Aggregation
79
.. contents:: On this page
810
:local:
911
:backlinks: none
10-
:depth: 1
12+
:depth: 2
1113
:class: singlecol
1214

1315
:manual:`Aggregation framework</aggregation/>`

source/reference/map-reduce.txt

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
**********
2+
Map-Reduce
3+
**********
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
:manual:`Map-Reduce </core/map-reduce/>` is a data processing paradigm for
14+
condensing large volumes of data into aggregated results.
15+
16+
.. note::
17+
18+
The :ref:`aggregation framework <aggregation>` provides better performance
19+
and usability than map-reduce operations, and should be preferred for
20+
new development.
21+
22+
A map-reduce operation is issued on a collection view, as obtained from
23+
``Collection#find`` method, by calling the ``map_reduce`` method on the
24+
view. The ``map_reduce`` method takes three arguments: the mapper, the
25+
reducer and map-reduce options. The mapper and the reducer must be provided
26+
as strings containing JavaScript functions.
27+
28+
For example, given the following collection with values 1 through 10:
29+
30+
.. code-block:: ruby
31+
32+
coll = client['foo']
33+
10.times do |i|
34+
coll.insert_one(v: i)
35+
end
36+
37+
The following invocation will sum up the values less than 6:
38+
39+
.. code-block:: ruby
40+
41+
coll.find(v: {'$lt' => 6}).map_reduce(
42+
'function() { emit(null, this.v) }',
43+
'function(key, values) { return Array.sum(values) }',
44+
).first['value']
45+
# => 15.0
46+
47+
The ``map_reduce`` method returns an instance of
48+
``Mongo::Collection::View::MapReduce`` - a map-reduce view which holds
49+
the parameters to be used for the operation. To execute the operation, either
50+
iterate the results (by using e.g. ``each``, ``first`` or ``to_a`` on the
51+
view object) or invoke the ``execute`` method. The ``execute`` method issues
52+
the map-reduce operation but does not return the result set from the server,
53+
and is primarily useful for when the output of the operation is directed to
54+
a collection as follows:
55+
56+
.. code-block:: ruby
57+
58+
coll.find(...).map_reduce(...).out('destination_collection').execute
59+
60+
Note that:
61+
62+
- If the results of map-reduce are not directed to a collection, they are
63+
said to be retrieved inline. In this case the entire result set must fit in
64+
the 16 MiB BSON document size limit.
65+
- If the results of map-reduce are directed to a collection, and the
66+
map-reduce view is iterated, the driver automatically retrieves the
67+
entire collection and returns its contents as the result set. The
68+
collection is retrieved without sorting. If map-reduce is performed into
69+
a collection that is not empty, the driver will return the documents
70+
as they exist in the collection after the map-reduce operation completes,
71+
which may include the documents that were in the collection prior to the
72+
map-reduce operation.
73+
74+
.. code-block:: ruby
75+
76+
coll.find(...).map_reduce(...).out('destination_collection').each do |doc|
77+
# ...
78+
end
79+
80+
coll.find(...).map_reduce(...).out(replace: 'destination_collection', db: 'db_name').each do |doc|
81+
# ...
82+
end
83+
84+
Given a map-reduce view, it can be configured using the following methods:
85+
86+
.. list-table::
87+
:header-rows: 1
88+
:widths: 20 80
89+
90+
* - Method
91+
- Description
92+
93+
* - ``js_mode``
94+
- Sets the ``jsMode`` flag for the operation.
95+
96+
* - ``out``
97+
- Directs the output to the specified collection, instead of returning
98+
the result set.
99+
100+
* - ``scope``
101+
- Sets the scope for the operation.
102+
103+
* - ``verbose``
104+
- Sets whether to include the timing information in the result.
105+
106+
The following accessor methods are defined on the view object:
107+
108+
.. list-table::
109+
:header-rows: 1
110+
:widths: 20 80
111+
112+
* - Method
113+
- Description
114+
115+
* - ``js_mode``
116+
- Returns the current ``jsMode`` flag value.
117+
118+
* - ``map_function``
119+
- Returns the map function as a string.
120+
121+
* - ``out``
122+
- Returns the current output location for the operation.
123+
124+
* - ``reduce_function``
125+
- Returns the reduce function as a string.
126+
127+
* - ``scope``
128+
- Returns the current scope for the operation.
129+
130+
* - ``verbose``
131+
- Returns whether to include the timing information in the result.

source/reference/working-with-data.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ implements for inserting, updating and retrieving data from MongoDB.
1616
bulk-operations
1717
projection
1818
aggregation
19+
map-reduce
1920
text-search
2021
geospatial-search
2122
query-cache

0 commit comments

Comments
 (0)