Class Bucket

java.lang.Object
dev.morphia.aggregation.stages.Stage
dev.morphia.aggregation.stages.Bucket

public class Bucket extends Stage
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.

Since server release
3.4
  • Method Details

    • bucket

      public static Bucket bucket()
      Creates a new bucket stage
      Returns:
      the new stage
      Since:
      2.2
      MongoDB documentation
      Aggregration Pipeline Stage: $bucket
      Since server release
      3.4
    • boundaries

      public Bucket boundaries(Object... 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.

      The specified values must be in ascending order and of the same type.

      Parameters:
      boundaries - the boundaries
      Returns:
      this
    • defaultValue

      public Bucket defaultValue(Object defaultValue)
      Optional. A literal that specifies the _id of an additional bucket that contains all documents whose groupBy expression result does not fall into a bucket specified by boundaries.

      If unspecified, each input document must resolve the groupBy expression to a value within one of the bucket ranges specified by boundaries or the operation throws an error.

      Parameters:
      defaultValue - the default value
      Returns:
      this
    • groupBy

      public Bucket groupBy(Object groupBy)
      An expression to group documents by. To specify a field path, prefix the field name with a dollar sign $ and enclose it in quotes.

      Unless $bucket includes a default specification, each input document must resolve the groupBy field path or expression to a value that falls within one of the ranges specified by the boundaries.

      Parameters:
      groupBy - the grouping expression
      Returns:
      this
    • outputField

      public Bucket outputField(String name, Expression value)
      Adds a field to the document that specifies the fields to include in the output documents in addition to the _id field. To specify the field to include, you must use accumulator expressions.
      Parameters:
      name - the new field name
      value - the value expression
      Returns:
      this