-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
aggregation.txt
121 lines (93 loc) · 4.29 KB
/
aggregation.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
===========
Aggregation
===========
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Aggregation operations process data records and return computed
results. Aggregation operations group values from multiple documents
together, and can perform a variety of operations on the grouped data
to return a single result. MongoDB provides three ways to perform
aggregation: the :ref:`aggregation pipeline
<aggregation-framework>`, the :ref:`map-reduce function
<aggregation-map-reduce>`, and :ref:`single purpose aggregation methods
<single-purpose-agg-operations>`.
.. _aggregation-framework:
Aggregation Pipeline
--------------------
MongoDB's :doc:`aggregation framework
</core/aggregation-pipeline>` is modeled on the concept of data
processing pipelines. Documents enter a multi-stage pipeline that
transforms the documents into an aggregated result.
The most basic pipeline stages provide *filters* that operate like
queries and *document transformations* that modify the form
of the output document.
Other pipeline operations provide tools for grouping and sorting
documents by specific field or fields as well as tools for aggregating
the contents of arrays, including arrays of documents. In addition,
pipeline stages can use :ref:`operators
<aggregation-expression-operators>` for tasks such as calculating the
average or concatenating a string.
The pipeline provides efficient data aggregation using native
operations within MongoDB, and is the preferred method for data
aggregation in MongoDB.
The aggregation pipeline can operate on a
:doc:`sharded collection </sharding>`.
The aggregation pipeline can use indexes to improve its performance
during some of its stages. In addition, the aggregation pipeline has an
internal optimization phase. See
:ref:`aggregation-pipeline-operators-and-performance` and
:doc:`/core/aggregation-pipeline-optimization` for details.
.. include:: /images/aggregation-pipeline.rst
.. _aggregation-map-reduce:
Map-Reduce
----------
MongoDB also provides :doc:`map-reduce </core/map-reduce>` operations
to perform aggregation. In general, map-reduce operations have two
phases: a *map* stage that processes each document and *emits* one or
more objects for each input document, and *reduce* phase that combines
the output of the map operation. Optionally, map-reduce can have a
*finalize* stage to make final modifications to the result. Like other
aggregation operations, map-reduce can specify a query condition to
select the input documents as well as sort and limit the results.
Map-reduce uses custom JavaScript functions to perform the map and
reduce operations, as well as the optional *finalize* operation. While
the custom JavaScript provide great flexibility compared to the
aggregation pipeline, in general, map-reduce is less efficient and more
complex than the aggregation pipeline.
Map-reduce can operate on a
:doc:`sharded collection </sharding>`. Map-reduce operations
can also output to a sharded collection. See
:doc:`/core/aggregation-pipeline-sharded-collections` and
:doc:`/core/map-reduce-sharded-collections` for details.
.. note:: Starting in MongoDB 2.4, certain :binary:`~bin.mongo` shell
functions and properties are inaccessible in map-reduce
operations. MongoDB 2.4 also provides support for multiple
JavaScript operations to run at the same time. Before MongoDB 2.4,
JavaScript code executed in a single thread, raising concurrency
issues for map-reduce.
.. include:: /images/map-reduce.rst
.. _single-purpose-agg-operations:
Single Purpose Aggregation Operations
-------------------------------------
MongoDB also provides :method:`db.collection.count()` and
:method:`db.collection.distinct()`.
All of these operations aggregate documents from a single collection.
While these operations provide simple access to common aggregation
processes, they lack the flexibility and capabilities of the
aggregation pipeline and map-reduce.
.. include:: /images/distinct.rst
Additional Features and Behaviors
---------------------------------
For a feature comparison of the aggregation pipeline,
map-reduce, and the special group functionality, see
:doc:`/reference/aggregation-commands-comparison`.
.. toctree::
:titlesonly:
:hidden:
/core/aggregation-pipeline
/core/map-reduce
/reference/aggregation