Perform an aggregation using the aggregation framework on this collection. The :meth:`aggregate` method obeys the :attr:`read_preference` of this :class:`Collection`, except when ``$out`` or ``$merge`` are used on MongoDB <5.0, in which case :attr:`~pymongo.r
(
self,
pipeline: _Pipeline,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
**kwargs: Any,
)
| 2942 | ) |
| 2943 | |
| 2944 | def aggregate( |
| 2945 | self, |
| 2946 | pipeline: _Pipeline, |
| 2947 | session: Optional[ClientSession] = None, |
| 2948 | let: Optional[Mapping[str, Any]] = None, |
| 2949 | comment: Optional[Any] = None, |
| 2950 | **kwargs: Any, |
| 2951 | ) -> CommandCursor[_DocumentType]: |
| 2952 | """Perform an aggregation using the aggregation framework on this |
| 2953 | collection. |
| 2954 | |
| 2955 | The :meth:`aggregate` method obeys the :attr:`read_preference` of this |
| 2956 | :class:`Collection`, except when ``$out`` or ``$merge`` are used on |
| 2957 | MongoDB <5.0, in which case |
| 2958 | :attr:`~pymongo.read_preferences.ReadPreference.PRIMARY` is used. |
| 2959 | |
| 2960 | .. note:: This method does not support the 'explain' option. Please |
| 2961 | use `PyMongoExplain <https://pypi.org/project/pymongoexplain/>`_ |
| 2962 | instead. An example is included in the `aggregation example <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/aggregation/#aggregation-example>`_ |
| 2963 | documentation. |
| 2964 | |
| 2965 | .. note:: The :attr:`~pymongo.collection.Collection.write_concern` of |
| 2966 | this collection is automatically applied to this operation. |
| 2967 | |
| 2968 | Cursors are closed automatically when they are exhausted (the last batch of data is retrieved from the database). |
| 2969 | If a cursor is not exhausted, it will be closed automatically upon garbage collection, which leaves resources open but unused for a potentially long period of time. |
| 2970 | To avoid this, best practice is to call :meth:`Cursor.close` when the cursor is no longer needed, |
| 2971 | or use the cursor in a with statement:: |
| 2972 | |
| 2973 | with collection.aggregate() as cursor: |
| 2974 | for operation in cursor: |
| 2975 | print(operation) |
| 2976 | |
| 2977 | :param pipeline: a list of aggregation pipeline stages |
| 2978 | :param session: a |
| 2979 | :class:`~pymongo.client_session.ClientSession`. |
| 2980 | :param let: A dict of parameter names and values. Values must be |
| 2981 | constant or closed expressions that do not reference document |
| 2982 | fields. Parameters can then be accessed as variables in an |
| 2983 | aggregate expression context (e.g. ``"$$var"``). This option is |
| 2984 | only supported on MongoDB >= 5.0. |
| 2985 | :param comment: A user-provided comment to attach to this |
| 2986 | command. |
| 2987 | :param kwargs: extra `aggregate command`_ parameters. |
| 2988 | |
| 2989 | All optional `aggregate command`_ parameters should be passed as |
| 2990 | keyword arguments to this method. Valid options include, but are not |
| 2991 | limited to: |
| 2992 | |
| 2993 | - `allowDiskUse` (bool): Enables writing to temporary files. When set |
| 2994 | to True, aggregation stages can write data to the _tmp subdirectory |
| 2995 | of the --dbpath directory. The default is False. |
| 2996 | - `maxTimeMS` (int): The maximum amount of time to allow the operation |
| 2997 | to run in milliseconds. |
| 2998 | - `batchSize` (int): The maximum number of documents to return per |
| 2999 | batch. Ignored if the connected mongod or mongos does not support |
| 3000 | returning aggregate results using a cursor. |
| 3001 | - `collation` (optional): An instance of |
nothing calls this directly
no test coverage detected