forked from mongodb/docs-php-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDBDatabase-aggregate.txt
176 lines (130 loc) · 5.15 KB
/
MongoDBDatabase-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
175
176
==============================
MongoDB\\Database::aggregate()
==============================
.. versionadded:: 1.5
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\Database::aggregate()
Runs a specified :manual:`admin/diagnostic pipeline
</reference/operator/aggregation-pipeline/#db-aggregate-stages>` which does
not require an underlying collection. For aggregations on collection data,
see :phpmethod:`MongoDB\Collection::aggregate()`.
.. code-block:: php
function aggregate(
array $pipeline,
array $options = []
): Traversable
Parameters
----------
``$pipeline`` : array
Specifies an :manual:`aggregation pipeline </core/aggregation-pipeline>`
operation.
``$options`` : array
An array specifying the desired options.
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Name
- Type
- Description
* - allowDiskUse
- boolean
- Enables writing to temporary files. When set to ``true``, aggregation
stages can write data to the ``_tmp`` sub-directory in the ``dbPath``
directory.
* - batchSize
- integer
- The maximum number of documents within each batch returned in a query result.
By default, the ``aggregate`` command has an initial batch size of
``101`` documents and a maximum size of 16 mebibytes (MiB) for each subsequent batch. This
option can enforce a smaller limit than 16 MiB, but not a larger
one. If you set ``batchSize`` to a limit that results in batches larger than
16 MiB, this option has no effect.
A batchSize of ``0`` means that the cursor will be established, but no documents
will be returned in the first batch. This may be useful for quickly returning a cursor
or failure from ``aggregate`` without doing significant server-side work.
* - bypassDocumentValidation
- boolean
- If ``true``, allows the write operation to circumvent document level
validation. Defaults to ``false``.
This only applies when using the :ref:`$out <agg-out>` and
:ref:`$out <agg-merge>` stages.
* - codec
- MongoDB\\Codec\\DocumentCodec
- .. include:: /includes/extracts/common-option-codec.rst
.. versionadded:: 1.17
* - collation
- array|object
- .. include:: /includes/extracts/common-option-collation.rst
* - comment
- mixed
- .. include:: /includes/extracts/common-option-comment.rst
The comment can be any valid BSON type for server versions 4.4 and
above. Earlier server versions only support string values.
* - explain
- boolean
- Specifies whether or not to return the information on the processing of
the pipeline.
* - hint
- string|array|object
- .. include:: /includes/extracts/common-option-hint.rst
* - let
- array|object
- .. include:: /includes/extracts/common-option-let.rst
.. versionadded:: 1.9
* - maxTimeMS
- integer
- .. include:: /includes/extracts/common-option-maxTimeMS.rst
* - readConcern
- :php:`MongoDB\Driver\ReadConcern <class.mongodb-driver-readconcern>`
- .. include:: /includes/extracts/database-option-readConcern.rst
* - readPreference
- :php:`MongoDB\Driver\ReadPreference <class.mongodb-driver-readpreference>`
- .. include:: /includes/extracts/database-option-readPreference.rst
* - session
- :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
- .. include:: /includes/extracts/common-option-session.rst
* - typeMap
- array
- .. include:: /includes/extracts/database-option-typeMap.rst
* - writeConcern
- :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
- .. include:: /includes/extracts/database-option-writeConcern.rst
Return Values
-------------
A :php:`MongoDB\Driver\Cursor <class.mongodb-driver-cursor>` or
:php:`ArrayIterator <arrayiterator>` object. In both cases, the return value
will be :php:`Traversable <traversable>`.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unexpectedvalueexception.rst
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
.. _php-db-agg-method-behavior:
Examples
--------
The following aggregation example lists all running commands using the
``$currentOp`` aggregation pipeline stage, then filters this list to only show
running command operations.
.. code-block:: php
<?php
$database = (new MongoDB\Client)->admin;
$cursor = $database->aggregate(
[
['$currentOp' => []],
['$match' => ['op' => 'command']],
]
);
See Also
--------
- :phpmethod:`MongoDB\Collection::aggregate()`
- :manual:`aggregate </reference/command/aggregate>` command reference in the
MongoDB manual
- :manual:`Aggregation Pipeline </core/aggregation-pipeline>` documentation in
the MongoDB Manual