Class Accumulators

java.lang.Object
com.mongodb.client.model.Accumulators

public final class Accumulators extends Object
Builders for accumulators used in the group pipeline stage of an aggregation pipeline.
Since:
3.1
MongoDB documentation
Aggregation pipeline
Accumulators
Expressions
Since server release
2.2
  • Method Summary

    Modifier and Type
    Method
    Description
    static BsonField
    accumulator(String fieldName, String initFunction, String accumulateFunction, String mergeFunction)
    Creates an $accumulator pipeline stage
    static BsonField
    accumulator(String fieldName, String initFunction, String accumulateFunction, String mergeFunction, String finalizeFunction)
    Creates an $accumulator pipeline stage
    static BsonField
    accumulator(String fieldName, String initFunction, String accumulateFunction, String mergeFunction, String finalizeFunction, String lang)
    Creates an $accumulator pipeline stage
    static BsonField
    accumulator(String fieldName, String initFunction, List<String> initArgs, String accumulateFunction, List<String> accumulateArgs, String mergeFunction, String finalizeFunction)
    Creates an $accumulator pipeline stage
    static BsonField
    accumulator(String fieldName, String initFunction, List<String> initArgs, String accumulateFunction, List<String> accumulateArgs, String mergeFunction, String finalizeFunction, String lang)
    Creates an $accumulator pipeline stage
    static <TExpression>
    BsonField
    addToSet(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing all unique values that results from applying the given expression to each document in a group of documents that share the same group by key.
    static <TExpression>
    BsonField
    avg(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the average of the values of the given expression when applied to all members of the group.
    static <OutExpression>
    BsonField
    bottom(String fieldName, Bson sortBy, OutExpression outExpression)
    Returns a combination of a computed field and an accumulator that produces a value of the given outExpression computed for the bottom element within a group sorted according to the provided sortBy specification.
    static <OutExpression, NExpression>
    BsonField
    bottomN(String fieldName, Bson sortBy, OutExpression outExpression, NExpression nExpression)
    Returns a combination of a computed field and an accumulator that produces a BSON Array of values of the given outExpression computed for the bottom N elements within a group sorted according to the provided sortBy specification, where N is the positive integral value of the nExpression.
    static <TExpression>
    BsonField
    first(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the value of the given expression when applied to the first member of the group.
    static <InExpression, NExpression>
    BsonField
    firstN(String fieldName, InExpression inExpression, NExpression nExpression)
    Returns a combination of a computed field and an accumulator that produces a BSON Array of values of the given inExpression computed for the first N elements within a presorted group, where N is the positive integral value of the nExpression.
    static <TExpression>
    BsonField
    last(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the value of the given expression when applied to the last member of the group.
    static <InExpression, NExpression>
    BsonField
    lastN(String fieldName, InExpression inExpression, NExpression nExpression)
    Returns a combination of a computed field and an accumulator that produces a BSON Array of values of the given inExpression computed for the last N elements within a presorted group, where N is the positive integral value of the nExpression.
    static <TExpression>
    BsonField
    max(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the maximum of the values of the given expression when applied to all members of the group.
    static <InExpression, NExpression>
    BsonField
    maxN(String fieldName, InExpression inExpression, NExpression nExpression)
    Returns a combination of a computed field and an accumulator that produces a BSON Array of N largest values of the given inExpression, where N is the positive integral value of the nExpression.
    static <TExpression>
    BsonField
    mergeObjects(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the result of merging the fields of the documents.
    static <TExpression>
    BsonField
    min(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the minimum of the values of the given expression when applied to all members of the group.
    static <InExpression, NExpression>
    BsonField
    minN(String fieldName, InExpression inExpression, NExpression nExpression)
    Returns a combination of a computed field and an accumulator that produces a BSON Array of N smallest values of the given inExpression, where N is the positive integral value of the nExpression.
    static <TExpression>
    BsonField
    push(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing an array of all values that results from applying an expression to each document in a group of documents that share the same group by key.
    static <TExpression>
    BsonField
    stdDevPop(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the sample standard deviation of the values of the given expression when applied to all members of the group.
    static <TExpression>
    BsonField
    stdDevSamp(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the sample standard deviation of the values of the given expression when applied to all members of the group.
    static <TExpression>
    BsonField
    sum(String fieldName, TExpression expression)
    Gets a field name for a $group operation representing the sum of the values of the given expression when applied to all members of the group.
    static <OutExpression>
    BsonField
    top(String fieldName, Bson sortBy, OutExpression outExpression)
    Returns a combination of a computed field and an accumulator that produces a value of the given outExpression computed for the top element within a group sorted according to the provided sortBy specification.
    static <OutExpression, NExpression>
    BsonField
    topN(String fieldName, Bson sortBy, OutExpression outExpression, NExpression nExpression)
    Returns a combination of a computed field and an accumulator that produces a BSON Array of values of the given outExpression computed for the top N elements within a group sorted according to the provided sortBy specification, where N is the positive integral value of the nExpression.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • sum

      public static <TExpression> BsonField sum(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the sum of the values of the given expression when applied to all members of the group.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      MongoDB documentation
      $sum
    • avg

      public static <TExpression> BsonField avg(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the average of the values of the given expression when applied to all members of the group.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      MongoDB documentation
      $avg
    • first

      public static <TExpression> BsonField first(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the value of the given expression when applied to the first member of the group.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      MongoDB documentation
      $first
    • firstN

      public static <InExpression, NExpression> BsonField firstN(String fieldName, InExpression inExpression, NExpression nExpression)
      Returns a combination of a computed field and an accumulator that produces a BSON Array of values of the given inExpression computed for the first N elements within a presorted group, where N is the positive integral value of the nExpression.
      Type Parameters:
      InExpression - The type of the input expression.
      NExpression - The type of the limiting expression.
      Parameters:
      fieldName - The field computed by the accumulator.
      inExpression - The input expression.
      nExpression - The expression limiting the number of produced values.
      Returns:
      The requested BsonField.
      Since:
      4.7
      MongoDB documentation
      $firstN
      Since server release
      5.2
    • top

      public static <OutExpression> BsonField top(String fieldName, Bson sortBy, OutExpression outExpression)
      Returns a combination of a computed field and an accumulator that produces a value of the given outExpression computed for the top element within a group sorted according to the provided sortBy specification.
      Type Parameters:
      OutExpression - The type of the output expression.
      Parameters:
      fieldName - The field computed by the accumulator.
      sortBy - The sort specification. The syntax is identical to the one expected by Aggregates.sort(Bson).
      outExpression - The output expression.
      Returns:
      The requested BsonField.
      Since:
      4.7
      MongoDB documentation
      $top
      Since server release
      5.2
    • topN

      public static <OutExpression, NExpression> BsonField topN(String fieldName, Bson sortBy, OutExpression outExpression, NExpression nExpression)
      Returns a combination of a computed field and an accumulator that produces a BSON Array of values of the given outExpression computed for the top N elements within a group sorted according to the provided sortBy specification, where N is the positive integral value of the nExpression.
      Type Parameters:
      OutExpression - The type of the output expression.
      NExpression - The type of the limiting expression.
      Parameters:
      fieldName - The field computed by the accumulator.
      sortBy - The sort specification. The syntax is identical to the one expected by Aggregates.sort(Bson).
      outExpression - The output expression.
      nExpression - The expression limiting the number of produced values.
      Returns:
      The requested BsonField.
      Since:
      4.7
      MongoDB documentation
      $topN
      Since server release
      5.2
    • last

      public static <TExpression> BsonField last(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the value of the given expression when applied to the last member of the group.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      MongoDB documentation
      $last
    • lastN

      public static <InExpression, NExpression> BsonField lastN(String fieldName, InExpression inExpression, NExpression nExpression)
      Returns a combination of a computed field and an accumulator that produces a BSON Array of values of the given inExpression computed for the last N elements within a presorted group, where N is the positive integral value of the nExpression.
      Type Parameters:
      InExpression - The type of the input expression.
      NExpression - The type of the limiting expression.
      Parameters:
      fieldName - The field computed by the accumulator.
      inExpression - The input expression.
      nExpression - The expression limiting the number of produced values.
      Returns:
      The requested BsonField.
      Since:
      4.7
      MongoDB documentation
      $lastN
      Since server release
      5.2
    • bottom

      public static <OutExpression> BsonField bottom(String fieldName, Bson sortBy, OutExpression outExpression)
      Returns a combination of a computed field and an accumulator that produces a value of the given outExpression computed for the bottom element within a group sorted according to the provided sortBy specification.
      Type Parameters:
      OutExpression - The type of the output expression.
      Parameters:
      fieldName - The field computed by the accumulator.
      sortBy - The sort specification. The syntax is identical to the one expected by Aggregates.sort(Bson).
      outExpression - The output expression.
      Returns:
      The requested BsonField.
      Since:
      4.7
      MongoDB documentation
      $bottom
      Since server release
      5.2
    • bottomN

      public static <OutExpression, NExpression> BsonField bottomN(String fieldName, Bson sortBy, OutExpression outExpression, NExpression nExpression)
      Returns a combination of a computed field and an accumulator that produces a BSON Array of values of the given outExpression computed for the bottom N elements within a group sorted according to the provided sortBy specification, where N is the positive integral value of the nExpression.
      Type Parameters:
      OutExpression - The type of the output expression.
      NExpression - The type of the limiting expression.
      Parameters:
      fieldName - The field computed by the accumulator.
      sortBy - The sort specification. The syntax is identical to the one expected by Aggregates.sort(Bson).
      outExpression - The output expression.
      nExpression - The expression limiting the number of produced values.
      Returns:
      The requested BsonField.
      Since:
      4.7
      MongoDB documentation
      $bottomN
      Since server release
      5.2
    • max

      public static <TExpression> BsonField max(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the maximum of the values of the given expression when applied to all members of the group.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      MongoDB documentation
      $max
    • maxN

      public static <InExpression, NExpression> BsonField maxN(String fieldName, InExpression inExpression, NExpression nExpression)
      Returns a combination of a computed field and an accumulator that produces a BSON Array of N largest values of the given inExpression, where N is the positive integral value of the nExpression.
      Type Parameters:
      InExpression - The type of the input expression.
      NExpression - The type of the limiting expression.
      Parameters:
      fieldName - The field computed by the accumulator.
      inExpression - The input expression.
      nExpression - The expression limiting the number of produced values.
      Returns:
      The requested BsonField.
      Since:
      4.7
      MongoDB documentation
      $maxN
      Since server release
      5.2
    • min

      public static <TExpression> BsonField min(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the minimum of the values of the given expression when applied to all members of the group.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      MongoDB documentation
      $min
    • minN

      public static <InExpression, NExpression> BsonField minN(String fieldName, InExpression inExpression, NExpression nExpression)
      Returns a combination of a computed field and an accumulator that produces a BSON Array of N smallest values of the given inExpression, where N is the positive integral value of the nExpression.
      Type Parameters:
      InExpression - The type of the input expression.
      NExpression - The type of the limiting expression.
      Parameters:
      fieldName - The field computed by the accumulator.
      inExpression - The input expression.
      nExpression - The expression limiting the number of produced values.
      Returns:
      The requested BsonField.
      Since:
      4.7
      MongoDB documentation
      $minN
      Since server release
      5.2
    • push

      public static <TExpression> BsonField push(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing an array of all values that results from applying an expression to each document in a group of documents that share the same group by key.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      MongoDB documentation
      $push
    • addToSet

      public static <TExpression> BsonField addToSet(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing all unique values that results from applying the given expression to each document in a group of documents that share the same group by key.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      MongoDB documentation
      $addToSet
    • mergeObjects

      public static <TExpression> BsonField mergeObjects(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the result of merging the fields of the documents. If documents to merge include the same field name, the field, in the resulting document, has the value from the last document merged for the field.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      Since:
      4.4
      MongoDB documentation
      $mergeObjects
    • stdDevPop

      public static <TExpression> BsonField stdDevPop(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the sample standard deviation of the values of the given expression when applied to all members of the group.

      Use if the values encompass the entire population of data you want to represent and do not wish to generalize about a larger population.

      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      Since:
      3.2
      MongoDB documentation
      $stdDevPop
      Since server release
      3.2
    • stdDevSamp

      public static <TExpression> BsonField stdDevSamp(String fieldName, TExpression expression)
      Gets a field name for a $group operation representing the sample standard deviation of the values of the given expression when applied to all members of the group.

      Use if the values encompass a sample of a population of data from which to generalize about the population.

      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the field
      Since:
      3.2
      MongoDB documentation
      $stdDevSamp
      Since server release
      3.2
    • accumulator

      public static BsonField accumulator(String fieldName, String initFunction, String accumulateFunction, String mergeFunction)
      Creates an $accumulator pipeline stage
      Parameters:
      fieldName - the field name
      initFunction - a function used to initialize the state
      accumulateFunction - a function used to accumulate documents
      mergeFunction - a function used to merge two internal states, e.g. accumulated on different shards or threads. It returns the resulting state of the accumulator.
      Returns:
      the $accumulator pipeline stage
      Since:
      4.1
      MongoDB documentation
      $accumulator
      Since server release
      4.4
    • accumulator

      public static BsonField accumulator(String fieldName, String initFunction, String accumulateFunction, String mergeFunction, @Nullable String finalizeFunction)
      Creates an $accumulator pipeline stage
      Parameters:
      fieldName - the field name
      initFunction - a function used to initialize the state
      accumulateFunction - a function used to accumulate documents
      mergeFunction - a function used to merge two internal states, e.g. accumulated on different shards or threads. It returns the resulting state of the accumulator.
      finalizeFunction - a function used to finalize the state and return the result (may be null)
      Returns:
      the $accumulator pipeline stage
      Since:
      4.1
      MongoDB documentation
      $accumulator
      Since server release
      4.4
    • accumulator

      public static BsonField accumulator(String fieldName, String initFunction, @Nullable List<String> initArgs, String accumulateFunction, @Nullable List<String> accumulateArgs, String mergeFunction, @Nullable String finalizeFunction)
      Creates an $accumulator pipeline stage
      Parameters:
      fieldName - the field name
      initFunction - a function used to initialize the state
      initArgs - init function’s arguments (may be null)
      accumulateFunction - a function used to accumulate documents
      accumulateArgs - additional accumulate function’s arguments (may be null). The first argument to the function is ‘state’.
      mergeFunction - a function used to merge two internal states, e.g. accumulated on different shards or threads. It returns the resulting state of the accumulator.
      finalizeFunction - a function used to finalize the state and return the result (may be null)
      Returns:
      the $accumulator pipeline stage
      Since:
      4.1
      MongoDB documentation
      $accumulator
      Since server release
      4.4
    • accumulator

      public static BsonField accumulator(String fieldName, String initFunction, String accumulateFunction, String mergeFunction, @Nullable String finalizeFunction, String lang)
      Creates an $accumulator pipeline stage
      Parameters:
      fieldName - the field name
      initFunction - a function used to initialize the state
      accumulateFunction - a function used to accumulate documents
      mergeFunction - a function used to merge two internal states, e.g. accumulated on different shards or threads. It returns the resulting state of the accumulator.
      finalizeFunction - a function used to finalize the state and return the result (may be null)
      lang - a language specifier
      Returns:
      the $accumulator pipeline stage
      Since:
      4.1
      MongoDB documentation
      $accumulator
      Since server release
      4.4
    • accumulator

      public static BsonField accumulator(String fieldName, String initFunction, @Nullable List<String> initArgs, String accumulateFunction, @Nullable List<String> accumulateArgs, String mergeFunction, @Nullable String finalizeFunction, String lang)
      Creates an $accumulator pipeline stage
      Parameters:
      fieldName - the field name
      initFunction - a function used to initialize the state
      initArgs - init function’s arguments (may be null)
      accumulateFunction - a function used to accumulate documents
      accumulateArgs - additional accumulate function’s arguments (may be null). The first argument to the function is ‘state’.
      mergeFunction - a function used to merge two internal states, e.g. accumulated on different shards or threads. It returns the resulting state of the accumulator.
      finalizeFunction - a function used to finalize the state and return the result (may be null)
      lang - a language specifier
      Returns:
      the $accumulator pipeline stage
      Since:
      4.1
      MongoDB documentation
      $accumulator
      Since server release
      4.4