MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / aggregate

Method aggregate

pymongo/synchronous/database.py:639–720  ·  view source on GitHub ↗

Perform a database-level aggregation. See the `aggregation pipeline`_ documentation for a list of stages that are supported. .. code-block:: python # Lists all operations currently running on the server. with client.admin.aggregate([{"$currentOp": {}}

(
        self, pipeline: _Pipeline, session: Optional[ClientSession] = None, **kwargs: Any
    )

Source from the content-addressed store, hash-verified

637 return coll
638
639 def aggregate(
640 self, pipeline: _Pipeline, session: Optional[ClientSession] = None, **kwargs: Any
641 ) -> CommandCursor[_DocumentType]:
642 """Perform a database-level aggregation.
643
644 See the `aggregation pipeline`_ documentation for a list of stages
645 that are supported.
646
647 .. code-block:: python
648
649 # Lists all operations currently running on the server.
650 with client.admin.aggregate([{"$currentOp": {}}]) as cursor:
651 for operation in cursor:
652 print(operation)
653
654 The :meth:`aggregate` method obeys the :attr:`read_preference` of this
655 :class:`Database`, except when ``$out`` or ``$merge`` are used, in
656 which case :attr:`~pymongo.read_preferences.ReadPreference.PRIMARY`
657 is used.
658
659 Cursors are closed automatically when they are exhausted (the last batch of data is retrieved from the database).
660 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.
661 To avoid this, best practice is to call :meth:`Cursor.close` when the cursor is no longer needed,
662 or use the cursor in a with statement.
663
664 .. note:: This method does not support the 'explain' option. Please
665 use :meth:`~pymongo.database.Database.command` instead.
666
667 .. note:: The :attr:`~pymongo.database.Database.write_concern` of
668 this collection is automatically applied to this operation.
669
670 :param pipeline: a list of aggregation pipeline stages
671 :param session: a
672 :class:`~pymongo.client_session.ClientSession`.
673 :param kwargs: extra `aggregate command`_ parameters.
674
675 All optional `aggregate command`_ parameters should be passed as
676 keyword arguments to this method. Valid options include, but are not
677 limited to:
678
679 - `allowDiskUse` (bool): Enables writing to temporary files. When set
680 to True, aggregation stages can write data to the _tmp subdirectory
681 of the --dbpath directory. The default is False.
682 - `maxTimeMS` (int): The maximum amount of time to allow the operation
683 to run in milliseconds.
684 - `batchSize` (int): The maximum number of documents to return per
685 batch. Ignored if the connected mongod or mongos does not support
686 returning aggregate results using a cursor.
687 - `collation` (optional): An instance of
688 :class:`~pymongo.collation.Collation`.
689 - `let` (dict): A dict of parameter names and values. Values must be
690 constant or closed expressions that do not reference document
691 fields. Parameters can then be accessed as variables in an
692 aggregate expression context (e.g. ``"$$var"``). This option is
693 only supported on MongoDB >= 5.0.
694
695 :return: A :class:`~pymongo.command_cursor.CommandCursor` over the result
696 set.

Callers

nothing calls this directly

Calls 4

_tmp_sessionMethod · 0.45
_retryable_readMethod · 0.45
get_read_preferenceMethod · 0.45

Tested by

no test coverage detected