Interface AggregationPipeline

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      <U> java.util.Iterator<U> aggregate​(java.lang.Class<U> target)
      Executes the pipeline and aggregates the output in to the type mapped by the target type using the default options as defined in AggregationOptions.
      <U> java.util.Iterator<U> aggregate​(java.lang.Class<U> target, com.mongodb.AggregationOptions options)
      Executes the pipeline and aggregates the output in to the type mapped by the target type.
      <U> java.util.Iterator<U> aggregate​(java.lang.Class<U> target, com.mongodb.AggregationOptions options, com.mongodb.ReadPreference readPreference)
      Executes the pipeline and aggregates the output in to the type mapped by the target type.
      <U> java.util.Iterator<U> aggregate​(java.lang.String collectionName, java.lang.Class<U> target, com.mongodb.AggregationOptions options, com.mongodb.ReadPreference readPreference)
      Executes the pipeline and aggregates the output in to the type mapped by the target type.
      AggregationPipeline bucket​(java.lang.String field, java.util.List<?> boundaries)
      Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries.
      AggregationPipeline bucket​(java.lang.String field, java.util.List<?> boundaries, BucketOptions options)
      Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries.
      AggregationPipeline bucketAuto​(java.lang.String field, int bucketCount)
      Categorizes incoming documents into a specific number of groups, called buckets, based on a specified expression.
      AggregationPipeline bucketAuto​(java.lang.String field, int bucketCount, BucketAutoOptions options)
      Categorizes incoming documents into a specific number of groups, called buckets, based on a specified expression.
      AggregationPipeline geoNear​(GeoNear geoNear)
      Returns an ordered stream of documents based on the proximity to a geospatial point.
      AggregationPipeline group​(Group... groupings)
      Groups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group .
      AggregationPipeline group​(java.lang.String id, Group... groupings)
      Groups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group.
      AggregationPipeline group​(java.util.List<Group> id, Group... groupings)  
      AggregationPipeline limit​(int count)
      Passes the first n documents unmodified to the pipeline where n is the specified limit.
      AggregationPipeline lookup​(java.lang.String from, java.lang.String localField, java.lang.String foreignField, java.lang.String as)
      Performs a left outer join to an unsharded collection in the same database to filter in documents from the "joined" collection for processing.
      AggregationPipeline match​(Query query)
      Filters the document stream to allow only matching documents to pass unmodified into the next pipeline stage.
      <U> java.util.Iterator<U> out​(java.lang.Class<U> target)
      Places the output of the aggregation in the collection mapped by the target type using the default options as defined in AggregationOptions.
      <U> java.util.Iterator<U> out​(java.lang.Class<U> target, com.mongodb.AggregationOptions options)
      Places the output of the aggregation in the collection mapped by the target type.
      <U> java.util.Iterator<U> out​(java.lang.String collectionName, java.lang.Class<U> target)
      Places the output of the aggregation in the collection mapped by the target type using the default options as defined in AggregationOptions.
      <U> java.util.Iterator<U> out​(java.lang.String collectionName, java.lang.Class<U> target, com.mongodb.AggregationOptions options)
      Places the output of the aggregation in the collection mapped by the target type.
      AggregationPipeline project​(Projection... projections)
      Reshapes each document in the stream, such as by adding new fields or removing existing fields.
      AggregationPipeline sample​(int sampleSize)
      Randomly selects the specified number of documents from the previous pipeline stage.
      AggregationPipeline skip​(int count)
      Skips the first n documents where n is the specified skip number and passes the remaining documents unmodified to the pipeline.
      AggregationPipeline sort​(Sort... sorts)
      Reorders the document stream by a specified sort key.
      AggregationPipeline sortByCount​(java.lang.String field)
      Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
      AggregationPipeline unwind​(java.lang.String field)
      Deconstructs an array field from the input documents to output a document for each element.
      AggregationPipeline unwind​(java.lang.String field, com.mongodb.client.model.UnwindOptions options)
      Deconstructs an array field from the input documents to output a document for each element.
    • Method Detail

      • aggregate

        <U> java.util.Iterator<U> aggregate​(java.lang.Class<U> target)
        Executes the pipeline and aggregates the output in to the type mapped by the target type using the default options as defined in AggregationOptions. Note: This return type will change to MongoCursor in 2.0 to allow for finer-grained control of iteration and resource management.
        Type Parameters:
        U - type of the results
        Parameters:
        target - The class to use when iterating over the results
        Returns:
        an iterator of the computed results
      • aggregate

        <U> java.util.Iterator<U> aggregate​(java.lang.Class<U> target,
                                            com.mongodb.AggregationOptions options)
        Executes the pipeline and aggregates the output in to the type mapped by the target type. Note: This return type will change to MongoCursor in 2.0 to allow for finer-grained control of iteration and resource management.
        Type Parameters:
        U - type of the results
        Parameters:
        target - The class to use when iterating over the results
        options - The options to apply to this aggregation
        Returns:
        an iterator of the computed results
      • aggregate

        <U> java.util.Iterator<U> aggregate​(java.lang.Class<U> target,
                                            com.mongodb.AggregationOptions options,
                                            com.mongodb.ReadPreference readPreference)
        Executes the pipeline and aggregates the output in to the type mapped by the target type. Note: This return type will change to MongoCursor in 2.0 to allow for finer-grained control of iteration and resource management.
        Type Parameters:
        U - type of the results
        Parameters:
        target - The class to use when iterating over the results
        options - The options to apply to this aggregation
        readPreference - The read preference to apply to this pipeline
        Returns:
        an iterator of the computed results
      • aggregate

        <U> java.util.Iterator<U> aggregate​(java.lang.String collectionName,
                                            java.lang.Class<U> target,
                                            com.mongodb.AggregationOptions options,
                                            com.mongodb.ReadPreference readPreference)
        Executes the pipeline and aggregates the output in to the type mapped by the target type. Note: This return type will change to MongoCursor in 2.0 to allow for finer-grained control of iteration and resource management.
        Type Parameters:
        U - type of the results
        Parameters:
        collectionName - The collection in which to store the results of the aggregation overriding the mapped value in target
        target - The class to use when iterating over the results
        options - The options to apply to this aggregation
        readPreference - The read preference to apply to this pipeline
        Returns:
        an iterator of the computed results
      • geoNear

        AggregationPipeline geoNear​(GeoNear geoNear)
        Returns an ordered stream of documents based on the proximity to a geospatial point. Incorporates the functionality of $match, $sort, and $limit for geospatial data. The output documents include an additional distance field and can include a location identifier field.
        Parameters:
        geoNear - the geospatial parameters to apply to the pipeline
        Returns:
        this
        MongoDB documentation
        reference/operator/aggregation/geoNear $geoNear
      • group

        AggregationPipeline group​(Group... groupings)
        Groups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group . Consumes all input documents and outputs one document per each distinct group. The output documents only contain the identifier field and, if specified, accumulated fields. The ID for this group is null.
        Parameters:
        groupings - the group definitions
        Returns:
        this
        MongoDB documentation
        reference/operator/aggregation/group $group
      • group

        AggregationPipeline group​(java.lang.String id,
                                  Group... groupings)
        Groups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group. Consumes all input documents and outputs one document per each distinct group. The output documents only contain the identifier field and, if specified, accumulated fields.
        Parameters:
        id - the ID of the group create
        groupings - the group definitions
        Returns:
        this
        MongoDB documentation
        reference/operator/aggregation/group $group
      • limit

        AggregationPipeline limit​(int count)
        Passes the first n documents unmodified to the pipeline where n is the specified limit. For each input document, outputs either one document (for the first n documents) or zero documents (after the first n documents).
        Parameters:
        count - the maximum number of documents to return
        Returns:
        this
        MongoDB documentation
        reference/operator/aggregation/limit $limit
      • lookup

        AggregationPipeline lookup​(java.lang.String from,
                                   java.lang.String localField,
                                   java.lang.String foreignField,
                                   java.lang.String as)
        Performs a left outer join to an unsharded collection in the same database to filter in documents from the "joined" collection for processing. The $lookup stage does an equality match between a field from the input documents with a field from the documents of the “joined” collection. 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:
        from - the collection to join
        localField - the field from the input documents
        foreignField - the field from the documents of the "from" collection
        as - the output array field
        Returns:
        this
        Since:
        1.2
        MongoDB documentation
        reference/operator/aggregation/lookup $lookup
      • match

        AggregationPipeline match​(Query query)
        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:
        query - the query to use when matching
        Returns:
        this
        MongoDB documentation
        reference/operator/aggregation/match $match
      • out

        <U> java.util.Iterator<U> out​(java.lang.Class<U> target)
        Places the output of the aggregation in the collection mapped by the target type using the default options as defined in AggregationOptions.
        Type Parameters:
        U - type of the results
        Parameters:
        target - The class to use when iterating over the results
        Returns:
        an iterator of the computed results
        MongoDB documentation
        reference/operator/aggregation/out $out
      • out

        <U> java.util.Iterator<U> out​(java.lang.Class<U> target,
                                      com.mongodb.AggregationOptions options)
        Places the output of the aggregation in the collection mapped by the target type.
        Type Parameters:
        U - type of the results
        Parameters:
        target - The class to use when iterating over the results
        options - The options to apply to this aggregation
        Returns:
        an iterator of the computed results
        MongoDB documentation
        reference/operator/aggregation/out $out
      • out

        <U> java.util.Iterator<U> out​(java.lang.String collectionName,
                                      java.lang.Class<U> target)
        Places the output of the aggregation in the collection mapped by the target type using the default options as defined in AggregationOptions.
        Type Parameters:
        U - type of the results
        Parameters:
        collectionName - The collection in which to store the results of the aggregation overriding the mapped value in target
        target - The class to use when iterating over the results
        Returns:
        an iterator of the computed results
        MongoDB documentation
        reference/operator/aggregation/out $out
      • out

        <U> java.util.Iterator<U> out​(java.lang.String collectionName,
                                      java.lang.Class<U> target,
                                      com.mongodb.AggregationOptions options)
        Places the output of the aggregation in the collection mapped by the target type.
        Type Parameters:
        U - type of the results
        Parameters:
        collectionName - The collection in which to store the results of the aggregation overriding the mapped value in target
        target - The class to use when iterating over the results
        options - The options to apply to this aggregation
        Returns:
        an iterator of the computed results
        MongoDB documentation
        reference/operator/aggregation/out $out
      • skip

        AggregationPipeline skip​(int count)
        Skips the first n documents where n is the specified skip number and passes the remaining documents unmodified to the pipeline. For each input document, outputs either zero documents (for the first n documents) or one document (if after the first n documents).
        Parameters:
        count - the number of documents to skip
        Returns:
        this
        MongoDB documentation
        reference/operator/aggregation/skip $skip
      • sort

        AggregationPipeline sort​(Sort... sorts)
        Reorders the document stream by a specified sort key. Only the order changes; the documents remain unmodified. For each input document, outputs one document.
        Parameters:
        sorts - the sorts to apply to this pipeline
        Returns:
        this
        MongoDB documentation
        reference/operator/aggregation/sort $sort
      • unwind

        AggregationPipeline unwind​(java.lang.String field)
        Deconstructs an array field from the input documents to output a document for each element. Each output document replaces the array with an element value. For each input document, outputs n documents where n is the number of array elements and can be zero for an empty array.
        Parameters:
        field - the field to unwind
        Returns:
        this
        MongoDB documentation
        reference/operator/aggregation/unwind $unwind
      • unwind

        AggregationPipeline unwind​(java.lang.String field,
                                   com.mongodb.client.model.UnwindOptions options)
        Deconstructs an array field from the input documents to output a document for each element. Each output document replaces the array with an element value. For each input document, outputs n documents where n is the number of array elements and can be zero for an empty array.
        Parameters:
        field - the field to unwind
        options - unwind options
        Returns:
        this
      • sortByCount

        AggregationPipeline sortByCount​(java.lang.String field)
        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.

        Parameters:
        field - the field to sort by count
        Returns:
        this
        Since:
        1.5
        MongoDB documentation
        reference/operator/aggregation/sortByCount $sortByCount
      • bucket

        AggregationPipeline bucket​(java.lang.String field,
                                   java.util.List<?> boundaries)
        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.

        Parameters:
        field - the field that used as groupby for bucketing
        boundaries - An array of values based on the groupBy expression that specify the boundaries for each bucket. Each adjacent pair of values acts as the inclusive lower boundary and the exclusive upper boundary for the bucket. You must specify at least two boundaries.
        Returns:
        this
        Since:
        1.5
        MongoDB documentation
        reference/operator/aggregation/bucket $bucket
      • bucket

        AggregationPipeline bucket​(java.lang.String field,
                                   java.util.List<?> boundaries,
                                   BucketOptions options)
        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.

        Parameters:
        field - the field that used as groupby for bucketing
        boundaries - An array of values based on the groupBy expression that specify the boundaries for each bucket. Each adjacent pair of values acts as the inclusive lower boundary and the exclusive upper boundary for the bucket. You must specify at least two boundaries.
        options - options that include defaultField for values that not match in any boundaries and also allow to specify output by using accumulator Accumulator
        Returns:
        this
        Since:
        1.5
        MongoDB documentation
        reference/operator/aggregation/bucket $bucket
      • bucketAuto

        AggregationPipeline bucketAuto​(java.lang.String field,
                                       int bucketCount)
        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:
        field - the field that used as groupby for auto bucketing
        bucketCount - the number of buckets into which input documents are grouped
        Returns:
        this
        Since:
        1.5
        MongoDB documentation
        reference/operator/aggregation/bucketAuto $bucketAuto
      • bucketAuto

        AggregationPipeline bucketAuto​(java.lang.String field,
                                       int bucketCount,
                                       BucketAutoOptions options)
        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:
        field - the field that used as groupby for auto bucketing
        bucketCount - the number of buckets into which input documents are grouped
        options - options that include granularity for buckets and output
        Returns:
        this
        Since:
        1.5
        MongoDB documentation
        reference/operator/aggregation/bucketAuto $bucketAuto