Bucket class

$bucket aggregation stage

Stage description

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.

Example:

Dart code:

Bucket(
  groupBy: Field('price'),
  boundaries: [0, 200, 400],
  defaultId: "Other",
  output: {
    'count': Sum(1),
    'titles': Push(Field('title'))
  }
).build()

Equivalent mongoDB aggregation stage:

{
 $bucket: {
   groupBy: "$price",
   boundaries: [ 0, 200, 400 ],
   default: "Other",
   output: {
     "count": { $sum: 1 },
     "titles" : { $push: "$title" }
   }
 }
}

https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/

Inheritance

Constructors

Bucket({required AggregationExpr groupBy, required List boundaries, dynamic defaultId, Map<String, Accumulator>? output})
Creates $bucket aggregation stage

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

build() Map<String, Object>
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited