MongoDBCollection-aggregate.txt 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
================================
MongoDB\\Collection::aggregate()
================================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16
.. phpmethod:: MongoDB\\Collection::aggregate()
17 18

   Executes an :manual:`aggregation framework pipeline
19
   </core/aggregation-pipeline>` operation on the collection.
20 21 22 23

   .. code-block:: php

      function aggregate(array $pipeline, array $options = []): Traversable
24

25
   This method has the following parameters:
26 27

   .. include:: /includes/apiargs/MongoDBCollection-method-aggregate-param.rst
28 29

   The ``$options`` parameter supports the following options:
30 31 32

   .. include:: /includes/apiargs/MongoDBCollection-method-aggregate-option.rst

33 34
Return Values
-------------
35

36 37 38
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>`.
39

40 41 42 43 44 45 46 47
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

48
.. _php-agg-method-behavior:
49

50 51 52 53 54 55 56 57 58 59
Behavior
--------

:phpmethod:`MongoDB\\Collection::aggregate()`'s return value depends on the
MongoDB server version and whether the ``useCursor`` option is specified. If
``useCursor`` is ``true``, a :php:`MongoDB\\Driver\\Cursor
<class.mongodb-driver-cursor>` object is returned. If ``useCursor`` is
``false``, an :php:`ArrayIterator <arrayiterator>` is returned that wraps the
``result`` array from the command response document. In both cases, the return
value will be :php:`Traversable <traversable>`.
60 61 62

.. todo: add examples

63 64
See Also
--------
65

66 67 68 69
- :manual:`aggregate </reference/command/aggregate>` command reference in the
  MongoDB manual
- :manual:`Aggregation Pipeline </core/aggregation-pipeline>` documentation in
  the MongoDB Manual