This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
/
Stage.php
367 lines (339 loc) · 10.5 KB
/
Stage.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
namespace Doctrine\MongoDB\Aggregation;
use Doctrine\MongoDB\Iterator;
use GeoJson\Geometry\Point;
/**
* Fluent interface for building aggregation pipelines.
*
* @author alcaeus <alcaeus@alcaeus.org>
* @since 1.2
*/
abstract class Stage
{
/**
* @var Builder
*/
protected $builder;
/**
* @param Builder $builder
*/
public function __construct(Builder $builder)
{
$this->builder = $builder;
}
/**
* Assembles the aggregation stage
*
* @return array
*/
abstract public function getExpression();
/**
* Executes the aggregation pipeline
*
* @param array $options
* @return Iterator
*/
public function execute($options = [])
{
return $this->builder->execute($options);
}
/**
* Categorizes incoming documents into groups, called buckets, based on a
* specified expression and bucket boundaries.
*
* Each bucket is represented as a document in the output. The document for
* each bucket contains an _id field, whose value specifies the inclusive
* lower bound of the bucket and a count field that contains the number of
* documents in the bucket. The count field is included by default when the
* output is not specified.
*
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/
*
* @return Stage\Bucket
*/
public function bucket()
{
return $this->builder->bucket();
}
/**
* Categorizes incoming documents into a specific number of groups, called
* buckets, based on a specified expression.
*
* Bucket boundaries are automatically determined in an attempt to evenly
* distribute the documents into the specified number of buckets. Each
* bucket is represented as a document in the output. The document for each
* bucket contains an _id field, whose value specifies the inclusive lower
* bound and the exclusive upper bound for the bucket, and a count field
* that contains the number of documents in the bucket. The count field is
* included by default when the output is not specified.
*
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucketAuto/
*
* @return Stage\BucketAuto
*/
public function bucketAuto()
{
return $this->builder->bucketAuto();
}
/**
* Returns statistics regarding a collection or view.
*
* $collStats must be the first stage in an aggregation pipeline, or else
* the pipeline returns an error.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/
* @since 1.5
*
* @return Stage\CollStats
*/
public function collStats()
{
return $this->builder->collStats();
}
/**
* Returns a document that contains a count of the number of documents input
* to the stage.
*
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/count/
*
* @return Stage\Count
*/
public function count($fieldName)
{
return $this->builder->count($fieldName);
}
/**
* Processes multiple aggregation pipelines within a single stage on the
* same set of input documents.
*
* Each sub-pipeline has its own field in the output document where its
* results are stored as an array of documents.
*
* @return Stage\Facet
*/
public function facet()
{
return $this->builder->facet();
}
/**
* Outputs documents in order of nearest to farthest from a specified point.
* You can only use this as the first stage of a pipeline.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/
*
* @param float|array|Point $x
* @param float $y
* @return Stage\GeoNear
*/
public function geoNear($x, $y = null)
{
return $this->builder->geoNear($x, $y);
}
/**
* Returns the assembled aggregation pipeline
*
* @return array
*/
public function getPipeline()
{
return $this->builder->getPipeline();
}
/**
* Performs a recursive search on a collection, with options for restricting
* the search by recursion depth and query filter.
*
* @see https://docs.mongodb.org/manual/reference/operator/aggregation/graphLookup/
*
* @param string $from Target collection for the $graphLookup operation to
* search, recursively matching the connectFromField to the connectToField.
* @return Stage\GraphLookup
*/
public function graphLookup($from)
{
return $this->builder->graphLookup($from);
}
/**
* Groups documents by some specified expression and outputs to the next
* stage a document for each distinct grouping.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/group/
*
* @return Stage\Group
*/
public function group()
{
return $this->builder->group();
}
/**
* Returns statistics regarding the use of each index for the collection.
*
* @see https://docs.mongodb.org/manual/reference/operator/aggregation/indexStats/
*
* @return Stage\IndexStats
*/
public function indexStats()
{
return $this->builder->indexStats();
}
/**
* Limits the number of documents passed to the next stage in the pipeline.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/limit/
*
* @param integer $limit
* @return Stage\Limit
*/
public function limit($limit)
{
return $this->builder->limit($limit);
}
/**
* Performs a left outer join to an unsharded collection in the same
* database to filter in documents from the “joined” collection for
* processing.
*
* @see https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/
*
* @param string $from
* @return Stage\Lookup
*/
public function lookup($from)
{
return $this->builder->lookup($from);
}
/**
* Filters the documents to pass only the documents that match the specified
* condition(s) to the next pipeline stage.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/match/
*
* @return Stage\Match
*/
public function match()
{
return $this->builder->match();
}
/**
* Takes the documents returned by the aggregation pipeline and writes them
* to a specified collection. This must be the last stage in the pipeline.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/out/
*
* @param string $collection
* @return Stage\Out
*/
public function out($collection)
{
return $this->builder->out($collection);
}
/**
* Passes along the documents with only the specified fields to the next
* stage in the pipeline. The specified fields can be existing fields from
* the input documents or newly computed fields.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/project/
*
* @return Stage\Project
*/
public function project()
{
return $this->builder->project();
}
/**
* Restricts the contents of the documents based on information stored in
* the documents themselves.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/redact/
*
* @return Stage\Redact
*/
public function redact()
{
return $this->builder->redact();
}
/**
* Promotes a specified document to the top level and replaces all other
* fields.
*
* The operation replaces all existing fields in the input document,
* including the _id field. You can promote an existing embedded document to
* the top level, or create a new document for promotion.
*
* @param string|null $expression Optional. A replacement expression that
* resolves to a document.
*
* @return Stage\ReplaceRoot
*/
public function replaceRoot($expression = null)
{
return $this->builder->replaceRoot($expression);
}
/**
* Randomly selects the specified number of documents from its input.
*
* @see https://docs.mongodb.org/manual/reference/operator/aggregation/sample/
*
* @param integer $size
* @return Stage\Sample
*/
public function sample($size)
{
return $this->builder->sample($size);
}
/**
* Skips over the specified number of documents that pass into the stage and
* passes the remaining documents to the next stage in the pipeline.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/skip/
*
* @param integer $skip
* @return Stage\Skip
*/
public function skip($skip)
{
return $this->builder->skip($skip);
}
/**
* Groups incoming documents based on the value of a specified expression,
* then computes the count of documents in each distinct group.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/sortByCount/
*
* @param string $expression The expression to group by
* @return Stage\SortByCount
*/
public function sortByCount($expression)
{
return $this->builder->sortByCount($expression);
}
/**
* Sorts all input documents and returns them to the pipeline in sorted order.
*
* If sorting by multiple fields, the first argument should be an array of
* field name (key) and order (value) pairs.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/sort/
*
* @param array|string $fieldName Field name or array of field/order pairs
* @param integer|string $order Field order (if one field is specified)
* @return Stage\Sort
*/
public function sort($fieldName, $order = null)
{
return $this->builder->sort($fieldName, $order);
}
/**
* Deconstructs an array field from the input documents to output a document
* for each element. Each output document is the input document with the
* value of the array field replaced by the element.
*
* @see http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/
*
* @param string $fieldName The field to unwind. It is automatically prefixed with the $ sign
* @return Stage\Unwind
*/
public function unwind($fieldName)
{
return $this->builder->unwind($fieldName);
}
}