Interface Aggregation<T>

  • Type Parameters:
    T - The initial type of the aggregation. Used for collection name resolution.
    All Known Implementing Classes:
    AggregationImpl

    public interface Aggregation<T>
    Since:
    2.0
    • Method Detail

      • addFields

        Aggregation<T> addFields​(AddFields fields)
        Adds new fields to documents. $addFields outputs documents that contain all existing fields from the input documents and newly added fields.

        The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

        Parameters:
        fields - the stage definition
        Returns:
        this
        Since server release
        3.4
        MongoDB documentation
        Aggregration Expression: $addFields
      • autoBucket

        Aggregation<T> autoBucket​(AutoBucket 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.

        Parameters:
        bucket - the bucket definition
        Returns:
        this
        Since server release
        3.4
        MongoDB documentation
        Aggregration Expression: $bucketAuto
      • bucket

        Aggregation<T> bucket​(Bucket bucket)
        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.

        $bucket only produces output documents for buckets that contain at least one input document.

        Parameters:
        bucket - the bucket definition
        Returns:
        this
        Since server release
        3.4
        MongoDB documentation
        Aggregration Expression: $bucket
      • count

        Aggregation<T> count​(String name)
        Passes a document to the next stage that contains a count of the number of documents input to the stage.
        Parameters:
        name - the field name for the resulting count value
        Returns:
        this
        Since server release
        3.4
        MongoDB documentation
        Aggregration Expression: $count
      • currentOp

        Aggregation<T> currentOp​(CurrentOp currentOp)
        Returns a stream of documents containing information on active and/or dormant operations as well as inactive sessions that are holding locks as part of a transaction. The stage returns a document for each operation or session. To run $currentOp, use the db.aggregate() helper on the admin database.

        The $currentOp aggregation stage is preferred over the currentOp command and its mongo shell helper db.currentOp(). Because currentOp command and db.currentOp() helper returns the results in a single document, the total size of the currentOp result set is subject to the maximum 16MB BSON size limit for documents. The $currentOp stage returns a cursor over a stream of documents, each of which reports a single operation. Each operation document is subject to the 16MB BSON limit, but unlike the currentOp command, there is no limit on the overall size of the result set.

        $currentOp also enables you to perform arbitrary transformations of the results as the documents pass through the pipeline.

        Parameters:
        currentOp - the configuration
        Returns:
        this
        Since server release
        3.6
        MongoDB documentation
        Aggregration Expression: $currentOp
      • execute

        <S> MorphiaCursor<S> execute​(Class<S> resultType)
        Execute the aggregation and get the results.
        Type Parameters:
        S - the output type
        Parameters:
        resultType - the type of the result
        Returns:
        a MorphiaCursor
      • execute

        <S> MorphiaCursor<S> execute​(Class<S> resultType,
                                     AggregationOptions options)
        Execute the aggregation and get the results.
        Type Parameters:
        S - the output type
        Parameters:
        resultType - the type of the result
        options - the options to apply
        Returns:
        a MorphiaCursor
      • facet

        Aggregation<T> facet​(Facet facet)
        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.

        The $facet stage allows you to create multi-faceted aggregations which characterize data across multiple dimensions, or facets, within a single aggregation stage. Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis. Retailers commonly use faceting to narrow search results by creating filters on product price, manufacturer, size, etc.

        Input documents are passed to the $facet stage only once. $facet enables various aggregations on the same set of input documents, without needing to retrieve the input documents multiple times.

        Parameters:
        facet - the facet definition
        Returns:
        this
        Since server release
        3.4
        MongoDB documentation
        Aggregration Expression: $facet
      • graphLookup

        Aggregation<T> graphLookup​(GraphLookup lookup)
        Performs a recursive search on a collection, with options for restricting the search by recursion depth and query filter.
        Parameters:
        lookup - the lookup configuration
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $graphLookup
      • group

        Aggregation<T> group​(Group group)
        Groups input documents by the specified _id expression and for each distinct grouping, outputs a document. The _id field of each output document contains the unique group by value. The output documents can also contain computed fields that hold the values of some accumulator expression.
        Parameters:
        group - the group definition
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $group
      • indexStats

        Aggregation<T> indexStats()
        Returns statistics regarding the use of each index for the collection. If running with access control, the user must have privileges that include indexStats action.
        Returns:
        this
        Since server release
        3.2
        MongoDB documentation
        Aggregration Expression: $indexStats
      • limit

        Aggregation<T> limit​(long limit)
        Limits the number of documents passed to the next stage in the pipeline.
        Parameters:
        limit - the maximum docs to pass along to the next stage
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $limit
      • lookup

        Aggregation<T> lookup​(Lookup lookup)
        Performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing. To each input document, the $lookup stage adds a new array field whose elements are the matching documents from the “joined” collection. The $lookup stage passes these reshaped documents to the next stage.
        Parameters:
        lookup - the lookup definition
        Returns:
        this
        Since server release
        3.6
        MongoDB documentation
        Aggregration Expression: $lookup
      • match

        Aggregation<T> match​(Filter... filters)
        Filters the document stream to allow only matching documents to pass unmodified into the next pipeline stage. $match uses standard MongoDB queries. For each input document, outputs either one document (a match) or zero documents (no match).
        Parameters:
        filters - the filters to use when matching
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $match
      • merge

        <M> void merge​(Merge<M> merge)
        Writes the results of the aggregation pipeline to a specified collection. The $merge operator must be the last stage in the pipeline.
        Type Parameters:
        M - the output collection type
        Parameters:
        merge - the merge definition
        Since server release
        4.2
        MongoDB documentation
        Aggregration Expression: $merge
      • merge

        <M> void merge​(Merge<M> merge,
                       AggregationOptions options)
        Writes the results of the aggregation pipeline to a specified collection. The $merge operator must be the last stage in the pipeline.
        Type Parameters:
        M - the output collection type
        Parameters:
        merge - the merge definition
        options - the options to apply
        Since server release
        3.4
        MongoDB documentation
        Aggregration Expression: $merge
      • out

        <O> void out​(Out<O> out)
        Writes the results of the aggregation pipeline to a specified collection. The $out operator must be the last stage in the pipeline.
        Type Parameters:
        O - the output collection type
        Parameters:
        out - the out definition
        MongoDB documentation
        Aggregration Expression: $out
      • out

        <O> void out​(Out<O> out,
                     AggregationOptions options)
        Writes the results of the aggregation pipeline to a specified collection. The $out operator must be the last stage in the pipeline.
        Type Parameters:
        O - the output collection type
        Parameters:
        out - the out definition
        options - the options to apply
        MongoDB documentation
        Aggregration Expression: $out
      • project

        Aggregation<T> project​(Projection projection)
        Passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.
        Parameters:
        projection - the project definition
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $project
      • redact

        Aggregation<T> redact​(Redact redact)
        Restricts the contents of the documents based on information stored in the documents themselves.
        Parameters:
        redact - the redaction definition
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $redact
      • replaceRoot

        Aggregation<T> replaceRoot​(ReplaceRoot root)
        Replaces the input document with the specified document. 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
        Parameters:
        root - the new root definition
        Returns:
        this
        Since server release
        3.4
        MongoDB documentation
        Aggregration Expression: $replaceRoot
      • replaceWith

        Aggregation<T> replaceWith​(ReplaceWith with)
        Replaces the input document with the specified document. The operation replaces all existing fields in the input document, including the _id field. With $replaceWith, you can promote an embedded document to the top-level. You can also specify a new document as the replacement.

        The $replaceWith is an alias for $replaceRoot.

        Parameters:
        with - the replacement definition
        Returns:
        this
        Since server release
        4.2
        MongoDB documentation
        Aggregration Expression: $replaceWith
      • sample

        Aggregation<T> sample​(long sample)
        Randomly selects the specified number of documents from the previous pipeline stage.
        Parameters:
        sample - the sample definition
        Returns:
        this
        Since server release
        3.2
        MongoDB documentation
        Aggregration Expression: $sample
      • set

        Aggregation<T> set​(Set set)
        Adds new fields to documents. $set outputs documents that contain all existing fields from the input documents and newly added fields.

        The $set stage is an alias for $addFields.

        Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

        Parameters:
        set - the stage to add
        Returns:
        this
        Since:
        2.3
        Since server release
        4.2
        MongoDB documentation
        Aggregration Expression: $set
      • set

        default Aggregation<T> set​(AddFields fields)
        Deprecated.
        Adds new fields to documents. $addFields outputs documents that contain all existing fields from the input documents and newly added fields.

        The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

        Parameters:
        fields - the stage definition
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $set
      • skip

        Aggregation<T> skip​(long skip)
        Skips over the specified number of documents that pass into the stage and passes the remaining documents to the next stage in the pipeline.
        Parameters:
        skip - the skip definition
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $skip
      • sortByCount

        Aggregation<T> sortByCount​(Expression sort)
        Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.

        Each output document contains two fields: an _id field containing the distinct grouping value, and a count field containing the number of documents belonging to that grouping or category.

        The documents are sorted by count in descending order.

        Parameters:
        sort - the sort definition
        Returns:
        this
        Since server release
        3.4
        MongoDB documentation
        Aggregration Expression: $sortByCount
      • unionWith

        Aggregation<T> unionWith​(Class<?> type,
                                 Stage first,
                                 Stage... others)
        Performs a union of two collections; i.e. $unionWith combines pipeline results from two collections into a single result set. The stage outputs the combined result set (including duplicates) to the next stage.
        Parameters:
        type - the type to perform the pipeline against
        first - the first pipeline stage
        others - the other pipeline stages
        Returns:
        this
        Since:
        2.1
        Since server release
        4.4
        MongoDB documentation
        Aggregration Expression: $unionWith
      • unionWith

        Aggregation<T> unionWith​(String collection,
                                 Stage first,
                                 Stage... others)
        Performs a union of two collections; i.e. $unionWith combines pipeline results from two collections into a single result set. The stage outputs the combined result set (including duplicates) to the next stage.
        Parameters:
        collection - the collection to perform the pipeline against
        first - the first pipeline stage
        others - the other pipeline stages
        Returns:
        this
        Since:
        2.1
        Since server release
        4.4
        MongoDB documentation
        Aggregration Expression: $unionWith
      • unwind

        Aggregation<T> unwind​(Unwind unwind)
        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.
        Parameters:
        unwind - the unwind definition
        Returns:
        this
        MongoDB documentation
        Aggregration Expression: $unwind