Class AggregationImpl<T>

  • Type Parameters:
    T -
    All Implemented Interfaces:
    Aggregation<T>

    @MorphiaInternal
    public class AggregationImpl<T>
    extends Object
    implements Aggregation<T>
    Since:
    2.0
    Developer note.
    This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
    • Constructor Detail

      • AggregationImpl

        @MorphiaInternal
        public AggregationImpl​(DatastoreImpl datastore,
                               MongoCollection<T> collection)
        Creates an instance.
        Parameters:
        datastore - the datastore
        collection - the source collection
        Developer note.
        This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • AggregationImpl

        @MorphiaInternal
        public AggregationImpl​(DatastoreImpl datastore,
                               Class<T> source,
                               MongoCollection<T> collection)
        Creates an instance.
        Parameters:
        datastore - the datastore
        source - the source type
        collection - the source collection
        Developer note.
        This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
    • Method Detail

      • autoBucket

        public Aggregation<T> autoBucket​(AutoBucket bucket)
        Description copied from interface: Aggregation
        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.

        Specified by:
        autoBucket in interface Aggregation<T>
        Parameters:
        bucket - the bucket definition
        Returns:
        this
      • bucket

        public Aggregation<T> bucket​(Bucket bucket)
        Description copied from interface: Aggregation
        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.

        Specified by:
        bucket in interface Aggregation<T>
        Parameters:
        bucket - the bucket definition
        Returns:
        this
      • count

        public Aggregation<T> count​(String name)
        Description copied from interface: Aggregation
        Passes a document to the next stage that contains a count of the number of documents input to the stage.
        Specified by:
        count in interface Aggregation<T>
        Parameters:
        name - the field name for the resulting count value
        Returns:
        this
      • currentOp

        public Aggregation<T> currentOp​(CurrentOp currentOp)
        Description copied from interface: Aggregation
        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.

        Specified by:
        currentOp in interface Aggregation<T>
        Parameters:
        currentOp - the configuration
        Returns:
        this
      • densify

        public Aggregation<T> densify​(Densify densify)
        Description copied from interface: Aggregation
        Creates new documents in a sequence of documents where certain values in a field are missing.
        Specified by:
        densify in interface Aggregation<T>
        Parameters:
        densify - the Densify stage
        Returns:
        this
      • documents

        public Aggregation<T> documents​(dev.morphia.aggregation.expressions.impls.DocumentExpression... documents)
        Description copied from interface: Aggregation
        Returns literal documents from input values.
        Specified by:
        documents in interface Aggregation<T>
        Parameters:
        documents - the documents to use
        Returns:
        this
      • execute

        public <R> dev.morphia.query.internal.MorphiaCursor<R> execute​(Class<R> resultType)
        Description copied from interface: Aggregation
        Execute the aggregation and get the results.
        Specified by:
        execute in interface Aggregation<T>
        Type Parameters:
        R - the output type
        Parameters:
        resultType - the type of the result
        Returns:
        a MorphiaCursor
      • execute

        public <R> dev.morphia.query.internal.MorphiaCursor<R> execute​(Class<R> resultType,
                                                                       AggregationOptions options)
        Description copied from interface: Aggregation
        Execute the aggregation and get the results.
        Specified by:
        execute in interface Aggregation<T>
        Type Parameters:
        R - the output type
        Parameters:
        resultType - the type of the result
        options - the options to apply
        Returns:
        a MorphiaCursor
      • facet

        public Aggregation<T> facet​(Facet facet)
        Description copied from interface: Aggregation
        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.

        Specified by:
        facet in interface Aggregation<T>
        Parameters:
        facet - the facet definition
        Returns:
        this
      • fill

        public Aggregation<T> fill​(Fill fill)
        Description copied from interface: Aggregation
        Populates null and missing field values within documents.

        You can use $fill to populate missing data points:

        • In a sequence based on surrounding values.
        • With a fixed value.
        Specified by:
        fill in interface Aggregation<T>
        Parameters:
        fill - the fill definition
        Returns:
        this
      • geoNear

        public Aggregation<T> geoNear​(GeoNear near)
        Description copied from interface: Aggregation
        Outputs documents in order of nearest to farthest from a specified point.
        Specified by:
        geoNear in interface Aggregation<T>
        Parameters:
        near - the geo query definition
        Returns:
        this
      • graphLookup

        public Aggregation<T> graphLookup​(GraphLookup lookup)
        Description copied from interface: Aggregation
        Performs a recursive search on a collection, with options for restricting the search by recursion depth and query filter.
        Specified by:
        graphLookup in interface Aggregation<T>
        Parameters:
        lookup - the lookup configuration
        Returns:
        this
      • group

        public Aggregation<T> group​(Group group)
        Description copied from interface: Aggregation
        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.
        Specified by:
        group in interface Aggregation<T>
        Parameters:
        group - the group definition
        Returns:
        this
      • indexStats

        public Aggregation<T> indexStats()
        Description copied from interface: Aggregation
        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.
        Specified by:
        indexStats in interface Aggregation<T>
        Returns:
        this
      • limit

        public Aggregation<T> limit​(long limit)
        Description copied from interface: Aggregation
        Limits the number of documents passed to the next stage in the pipeline.
        Specified by:
        limit in interface Aggregation<T>
        Parameters:
        limit - the maximum docs to pass along to the next stage
        Returns:
        this
      • lookup

        public Aggregation<T> lookup​(Lookup lookup)
        Description copied from interface: Aggregation
        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.
        Specified by:
        lookup in interface Aggregation<T>
        Parameters:
        lookup - the lookup definition
        Returns:
        this
      • match

        public Aggregation<T> match​(Filter... filters)
        Description copied from interface: Aggregation
        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).
        Specified by:
        match in interface Aggregation<T>
        Parameters:
        filters - the filters to use when matching
        Returns:
        this
      • merge

        public <M> void merge​(Merge<M> merge)
        Description copied from interface: Aggregation
        Writes the results of the aggregation pipeline to a specified collection. The $merge operator must be the last stage in the pipeline.
        Specified by:
        merge in interface Aggregation<T>
        Type Parameters:
        M - the output collection type
        Parameters:
        merge - the merge definition
      • merge

        public <M> void merge​(Merge<M> merge,
                              AggregationOptions options)
        Description copied from interface: Aggregation
        Writes the results of the aggregation pipeline to a specified collection. The $merge operator must be the last stage in the pipeline.
        Specified by:
        merge in interface Aggregation<T>
        Type Parameters:
        M - the output collection type
        Parameters:
        merge - the merge definition
        options - the options to apply
      • out

        public <O> void out​(Out<O> out)
        Description copied from interface: Aggregation
        Writes the results of the aggregation pipeline to a specified collection. The $out operator must be the last stage in the pipeline.
        Specified by:
        out in interface Aggregation<T>
        Type Parameters:
        O - the output collection type
        Parameters:
        out - the out definition
      • out

        public <O> void out​(Out<O> out,
                            AggregationOptions options)
        Description copied from interface: Aggregation
        Writes the results of the aggregation pipeline to a specified collection. The $out operator must be the last stage in the pipeline.
        Specified by:
        out in interface Aggregation<T>
        Type Parameters:
        O - the output collection type
        Parameters:
        out - the out definition
        options - the options to apply
      • planCacheStats

        public Aggregation<T> planCacheStats()
        Description copied from interface: Aggregation
        Returns plan cache information for a collection. The stage returns a document for each plan cache entry.
        Specified by:
        planCacheStats in interface Aggregation<T>
        Returns:
        this
      • project

        public Aggregation<T> project​(Projection projection)
        Description copied from interface: Aggregation
        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.
        Specified by:
        project in interface Aggregation<T>
        Parameters:
        projection - the project definition
        Returns:
        this
      • redact

        public Aggregation<T> redact​(Redact redact)
        Description copied from interface: Aggregation
        Restricts the contents of the documents based on information stored in the documents themselves.
        Specified by:
        redact in interface Aggregation<T>
        Parameters:
        redact - the redaction definition
        Returns:
        this
      • replaceRoot

        public Aggregation<T> replaceRoot​(ReplaceRoot root)
        Description copied from interface: Aggregation
        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
        Specified by:
        replaceRoot in interface Aggregation<T>
        Parameters:
        root - the new root definition
        Returns:
        this
      • replaceWith

        public Aggregation<T> replaceWith​(ReplaceWith with)
        Description copied from interface: Aggregation
        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.

        Specified by:
        replaceWith in interface Aggregation<T>
        Parameters:
        with - the replacement definition
        Returns:
        this
      • sample

        public Aggregation<T> sample​(long sample)
        Description copied from interface: Aggregation
        Randomly selects the specified number of documents from the previous pipeline stage.
        Specified by:
        sample in interface Aggregation<T>
        Parameters:
        sample - the sample definition
        Returns:
        this
      • addFields

        public Aggregation<T> addFields​(AddFields fields)
        Description copied from interface: Aggregation
        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.

        Specified by:
        addFields in interface Aggregation<T>
        Parameters:
        fields - the stage definition
        Returns:
        this
      • set

        public Aggregation<T> set​(Set set)
        Description copied from interface: Aggregation
        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.

        Specified by:
        set in interface Aggregation<T>
        Parameters:
        set - the stage to add
        Returns:
        this
      • skip

        public Aggregation<T> skip​(long skip)
        Description copied from interface: Aggregation
        Skips over the specified number of documents that pass into the stage and passes the remaining documents to the next stage in the pipeline.
        Specified by:
        skip in interface Aggregation<T>
        Parameters:
        skip - the skip definition
        Returns:
        this
      • sort

        public Aggregation<T> sort​(Sort sort)
        Description copied from interface: Aggregation
        Sorts all input documents and returns them to the pipeline in sorted order.
        Specified by:
        sort in interface Aggregation<T>
        Parameters:
        sort - the sort definition
        Returns:
        this
      • sortByCount

        public Aggregation<T> sortByCount​(dev.morphia.aggregation.expressions.impls.Expression sort)
        Description copied from interface: Aggregation
        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.

        Specified by:
        sortByCount in interface Aggregation<T>
        Parameters:
        sort - the sort definition
        Returns:
        this
      • unionWith

        public Aggregation<T> unionWith​(Class<?> type,
                                        Stage first,
                                        Stage... others)
        Description copied from interface: Aggregation
        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.
        Specified by:
        unionWith in interface Aggregation<T>
        Parameters:
        type - the type to perform the pipeline against
        first - the first pipeline stage
        others - the other pipeline stages
        Returns:
        this
      • unionWith

        public Aggregation<T> unionWith​(String collection,
                                        Stage first,
                                        Stage... others)
        Description copied from interface: Aggregation
        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.
        Specified by:
        unionWith in interface Aggregation<T>
        Parameters:
        collection - the collection to perform the pipeline against
        first - the first pipeline stage
        others - the other pipeline stages
        Returns:
        this
      • unset

        public Aggregation<T> unset​(Unset unset)
        Description copied from interface: Aggregation
        Removes/excludes fields from documents. Names must not start with '$'.
        Specified by:
        unset in interface Aggregation<T>
        Parameters:
        unset - the unset definition
        Returns:
        this
      • unwind

        public Aggregation<T> unwind​(Unwind unwind)
        Description copied from interface: Aggregation
        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.
        Specified by:
        unwind in interface Aggregation<T>
        Parameters:
        unwind - the unwind definition
        Returns:
        this
      • changeStream

        public Aggregation<T> changeStream()
        Description copied from interface: Aggregation
        Returns a Change Stream cursor on a collection, a database, or an entire cluster. Must be used as the first stage in an aggregation pipeline.
        Specified by:
        changeStream in interface Aggregation<T>
        Returns:
        this
      • changeStream

        public Aggregation<T> changeStream​(ChangeStream stream)
        Description copied from interface: Aggregation
        Returns a Change Stream cursor on a collection, a database, or an entire cluster. Must be used as the first stage in an aggregation pipeline.
        Specified by:
        changeStream in interface Aggregation<T>
        Parameters:
        stream - the options to apply to the stage
        Returns:
        this
      • addStage

        public Aggregation<T> addStage​(Stage stage)
        Description copied from interface: Aggregation
        Adds a custom stage to this pipeline. Sometimes a new server release comes out with new stages and Morphia can lag sometimes. Using this method, custom stages can be added to fill that gap until Morphia can catch up. This method is marked as internal because while it should remain relatively stable, it may shift as needs evolve and isn't intended for general use.
        Specified by:
        addStage in interface Aggregation<T>
        Parameters:
        stage - the new Stage
        Returns:
        this