-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdb.aggregate.txt
174 lines (116 loc) · 5.41 KB
/
db.aggregate.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
==============
db.aggregate()
==============
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.aggregate()
Runs a specified admin/diagnostic pipeline which does not require an
underlying collection. For aggregations on collection data, see
:method:`db.collection.aggregate()`.
.. |dbcommand| replace:: :dbcommand:`aggregate` command
.. include:: /includes/fact-mongosh-shell-method-alt.rst
The :method:`db.aggregate()` method has the following syntax:
.. code-block:: javascript
db.aggregate( [ <pipeline> ], { <options> } )
The ``pipeline`` parameter is an array of stages to execute. It
must start with a compatible stage that does not require an
underlying collection, such as :pipeline:`$currentOp` or
:pipeline:`$listLocalSessions`.
The ``options`` document can contain the following fields and values:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``explain``
- boolean
- Optional. Specifies that the method should return
information on the processing of the pipeline.
See :ref:`example-aggregate-method-explain-option` for an example.
Not available in :ref:`multi-document transactions <transactions>`.
* - ``allowDiskUse``
- boolean
- Optional. Enables writing to temporary files. When set to ``true``, aggregation
operations can write data to the :file:`_tmp` subdirectory in the
:setting:`~storage.dbPath` directory. See
:ref:`example-aggregate-method-external-sort` for an example.
.. include:: /includes/extracts/4.2-changes-usedDisk.rst
* - ``cursor``
- document
- Optional. Specifies the *initial* batch size for the cursor. The value of the ``cursor``
field is a document with the field ``batchSize``. See
:ref:`example-aggregate-method-initial-batch-size` for syntax and example.
* - ``maxTimeMS``
- non-negative integer
- Optional. Specifies a time limit in milliseconds for processing
operations on a cursor. If you do not specify a value for maxTimeMS,
operations will not time out. A value of ``0`` explicitly
specifies the default unbounded behavior.
MongoDB terminates operations that exceed their allotted time limit
using the same mechanism as :method:`db.killOp()`. MongoDB only
terminates an operation at one of its designated :term:`interrupt
points <interrupt point>`.
* - ``bypassDocumentValidation``
- boolean
- Optional. Applicable only if you specify the :pipeline:`$out` or :pipeline:`$merge` aggregation
stages.
Enables :method:`db.collection.aggregate` to bypass schema validation
during the operation. This lets you insert documents that do not
meet the validation requirements.
* - ``readConcern``
- document
- Optional. Specifies the :term:`read concern`.
.. include:: /includes/fact-readConcern-syntax.rst
.. include:: /includes/fact-readConcern-option-description.rst
.. include:: /includes/extracts/4.2-changes-out-linearizable.rst
.. include:: /includes/extracts/4.2-changes-linearizable-merge-restriction.rst
* - ``collation``
- document
- Optional.
.. include:: /includes/extracts/collation-option.rst
* - ``hint``
- string or document
- Optional. The index to use for the aggregation. The index is on the initial
collection/view against which the aggregation is run.
Specify the index either by the index name or by the index
specification document.
The ``hint`` does not apply to :pipeline:`$lookup` and
:pipeline:`$graphLookup` stages.
* - ``comment``
- string
- Optional. Users can specify an arbitrary string to help trace the operation
through the database profiler, currentOp, and logs.
* - ``writeConcern``
- document
- Optional. A document that expresses the :ref:`write concern <write-concern>`
to use with the :pipeline:`$out` or :pipeline:`$merge` stage.
Omit to use the default write concern with the :pipeline:`$out` or
:pipeline:`$merge` stage.
Compatibility
-------------
This method is available in deployments hosted in the following environments:
.. include:: /includes/fact-environments-atlas-only.rst
.. include:: /includes/fact-environments-atlas-support-all.rst
.. include:: /includes/fact-environments-onprem-only.rst
Example
-------
.. _admin-pipeline-currentOp:
Pipeline with ``$currentOp``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following example runs a pipeline with two stages. The first stage
runs the :pipeline:`$currentOp` operation and the second stage filters the
results of that operation.
.. code-block:: javascript
use admin
db.aggregate( [ {
$currentOp : { allUsers: true, idleConnections: true } }, {
$match : { shard: "shard01" }
}
] )