File: aggregate.xml

package info (click to toggle)
php-doc 20140201-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 74,084 kB
  • ctags: 4,040
  • sloc: xml: 998,137; php: 20,812; cpp: 500; sh: 177; makefile: 63; awk: 28
file content (436 lines) | stat: -rw-r--r-- 9,468 bytes parent folder | download
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 331062 $ -->

<refentry xml:id="mongocollection.aggregate" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>MongoCollection::aggregate</refname>
  <refpurpose>Perform an aggregation using the aggregation framework</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <modifier>public</modifier> <type>array</type><methodname>MongoCollection::aggregate</methodname>
   <methodparam><type>array</type><parameter>pipeline</parameter></methodparam>
   <methodparam choice="opt"><type>array</type><parameter>op</parameter></methodparam>
   <methodparam choice="opt"><type>array</type><parameter>...</parameter></methodparam>
  </methodsynopsis>
  <para>
   The MongoDB
   <link xlink:href="&url.mongodb.docs.aggregation;">aggregation framework</link>
   provides a means to calculate aggregated values without having to use
   MapReduce. While MapReduce is powerful, it is often more difficult than
   necessary for many simple aggregation tasks, such as totaling or averaging
   field values.
  </para>
  <para>
   This method accepts either a variable amount of pipeline operators, or a
   single array of operators constituting the pipeline.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <variablelist>
   <varlistentry>
    <term><parameter>pipeline</parameter></term>
    <listitem>
     <para>
      An array of pipeline operators, or just the first operator.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term><parameter>op</parameter></term>
    <listitem>
     <para>
      The second pipeline operator.
     </para>
    </listitem>
   </varlistentry>
   <varlistentry>
    <term><parameter>...</parameter></term>
    <listitem>
     <para>
      Additional pipeline operators.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   The result of the aggregation as an array. The <varname>ok</varname> will
   be set to <literal>1</literal> on success, <literal>0</literal> on failure.
  </para>
 </refsect1>

 <refsect1 role="errors">
  &reftitle.errors;
  <para>
   When an error occurs an array with the following keys will be returned:
   <itemizedlist>
    <listitem>
     <simpara>
      <varname>errmsg</varname> - containing the reason for the failure
     </simpara>
    </listitem>
    <listitem>
     <simpara>
      <varname>code</varname> - the errorcode of the failure
     </simpara>
    </listitem>
    <listitem>
     <simpara>
      <varname>ok</varname> - will be set to 0.
     </simpara>
    </listitem>
   </itemizedlist>
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <example xml:id="mongocollection.aggregate.example.basic">
   <title><methodname>MongoCollection::aggregate</methodname> example</title>
   <para>
    The following example aggregation operation pivots data to create a set of
    author names grouped by tags applied to an article. Call the aggregation
    framework by issuing the following command:
   </para>
   <programlisting role="php">
<![CDATA[
<?php
$m = new Mongo;
$c = $m->selectDB("examples")->selectCollection("article");
$data = array (
    'title' => 'this is my title',
    'author' => 'bob',
    'posted' => new MongoDate,
    'pageViews' => 5,
    'tags' => array ( 'fun', 'good', 'fun' ),
    'comments' => array (
      array (
        'author' => 'joe',
        'text' => 'this is cool',
      ),
      array (
        'author' => 'sam',
        'text' => 'this is bad',
      ),
    ),
    'other' =>array (
      'foo' => 5,
    ),
);
$d = $c->insert($data, array("w" => 1));

$ops = array(
    array(
        '$project' => array(
            "author" => 1,
            "tags"   => 1,
        )
    ),
    array('$unwind' => '$tags'),
    array(
        '$group' => array(
            "_id" => array("tags" => '$tags'),
            "authors" => array('$addToSet' => '$author'),
        ),
    ),
);
$results = $c->aggregate($ops);
var_dump($results);
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
array(2) {
  ["result"]=>
  array(2) {
    [0]=>
    array(2) {
      ["_id"]=>
      array(1) {
        ["tags"]=>
        string(4) "good"
      }
      ["authors"]=>
      array(1) {
        [0]=>
        string(3) "bob"
      }
    }
    [1]=>
    array(2) {
      ["_id"]=>
      array(1) {
        ["tags"]=>
        string(3) "fun"
      }
      ["authors"]=>
      array(1) {
        [0]=>
        string(3) "bob"
      }
    }
  }
  ["ok"]=>
  float(1)
}
]]>
   </screen>
  </example>

  <para>
   The following examples use the <link xlink:href="&url.mongodb.examples.zipcode;">zipcode data set</link>.
   Use mongoimport to load this data set into your mongod instance.
  </para>

  <example xml:id="mongocollection.aggregate.example.zipcode.population">
   <title><methodname>MongoCollection::aggregate</methodname> example</title>
   <para>
    To return all states with a population greater than 10 million, use the following aggregation operation:
   </para>
   <programlisting role="php">
<![CDATA[
<?php
$m = new Mongo;
$c = $m->selectDB("test")->selectCollection("zips");

$out = $c->aggregate(array(
        '$group' => array(
            '_id' => '$state',
           'totalPop' => array('$sum' => '$pop')
        )
    ),
    array(
        '$match' => array('totalPop' => array('$gte' => 10*1000*1000))
    )
);

var_dump($out);
?>
]]>
   </programlisting>
   &example.outputs.similar;
   <screen>
<![CDATA[
array(2) {
  ["result"]=>
  array(7) {
    [0]=>
    array(2) {
      ["_id"]=>
      string(2) "TX"
      ["totalPop"]=>
      int(16986510)
    }
    [1]=>
    array(2) {
      ["_id"]=>
      string(2) "PA"
      ["totalPop"]=>
      int(11881643)
    }
    [2]=>
    array(2) {
      ["_id"]=>
      string(2) "NY"
      ["totalPop"]=>
      int(17990455)
    }
    [3]=>
    array(2) {
      ["_id"]=>
      string(2) "IL"
      ["totalPop"]=>
      int(11430602)
    }
    [4]=>
    array(2) {
      ["_id"]=>
      string(2) "CA"
      ["totalPop"]=>
      int(29760021)
    }
    [5]=>
    array(2) {
      ["_id"]=>
      string(2) "OH"
      ["totalPop"]=>
      int(10847115)
    }
    [6]=>
    array(2) {
      ["_id"]=>
      string(2) "FL"
      ["totalPop"]=>
      int(12937926)
    }
  }
  ["ok"]=>
  float(1)
}
]]>
   </screen>
  </example>

  <example xml:id="mongocollection.aggregate.example.zipcode.population.average">
   <title><methodname>MongoCollection::aggregate</methodname> example</title>
   <para>
    To return the average populations for cities in each state, use the following aggregation operation:
   </para>
   <programlisting role="php">
<![CDATA[
<?php
$m = new Mongo;
$c = $m->selectDB("test")->selectCollection("zips");

$out = $c->aggregate(
    array(
        '$group' => array(
            '_id' => array('state' => '$state', 'city' => '$city' ),
            'pop' => array('$sum' => '$pop' )
        )
    ),
    array(
        '$group' => array(
            '_id' => '$_id.state',
            'avgCityPop' => array('$avg' => '$pop')
        )
    )
);

var_dump($out);
?>
]]>
   </programlisting>
   &example.outputs.similar;
   <screen>
<![CDATA[
array(2) {
  ["result"]=>
  array(51) {
    [0]=>
    array(2) {
      ["_id"]=>
      string(2) "DC"
      ["avgCityPop"]=>
      float(303450)
    }
    [1]=>
    array(2) {
      ["_id"]=>
      string(2) "DE"
      ["avgCityPop"]=>
      float(14481.913043478)
    }
    [2]=>
    array(2) {
      ["_id"]=>
      string(2) "RI"
      ["avgCityPop"]=>
      float(18933.283018868)
    }
    [3]=>
    array(2) {
      ["_id"]=>
      string(2) "AL"
      ["avgCityPop"]=>
      float(7907.2152641879)
    }
    [4]=>
    array(2) {
      ["_id"]=>
      string(2) "NH"
      ["avgCityPop"]=>
      float(5232.320754717)
    }
...
    [45]=>
    array(2) {
      ["_id"]=>
      string(2) "WY"
      ["avgCityPop"]=>
      float(3359.9111111111)
    }
    [46]=>
    array(2) {
      ["_id"]=>
      string(2) "MN"
      ["avgCityPop"]=>
      float(5335.4865853659)
    }
    [47]=>
    array(2) {
      ["_id"]=>
      string(2) "OK"
      ["avgCityPop"]=>
      float(6155.7436399217)
    }
    [48]=>
    array(2) {
      ["_id"]=>
      string(2) "IL"
      ["avgCityPop"]=>
      float(9931.0182450043)
    }
    [49]=>
    array(2) {
      ["_id"]=>
      string(2) "WI"
      ["avgCityPop"]=>
      float(7323.0074850299)
    }
    [50]=>
    array(2) {
      ["_id"]=>
      string(2) "WV"
      ["avgCityPop"]=>
      float(2759.1953846154)
    }
  }
  ["ok"]=>
  float(1)
}
]]>
   </screen>
  </example>

 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <simplelist>
   <member>The MongoDB <link xlink:href="&url.mongodb.docs.aggregation;">aggregation framework</link></member>
  </simplelist>
 </refsect1>

</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->