Skip to main content

Aggregate

Aggregation Operation Example for Database Collections

Aggregate.addFields(object: Object): Aggregate

Description: Aggregation stage. Adds new fields to the output records. After the addFields aggregation stage, all output records will include the fields specified by addFields in addition to the input fields.

Aggregate.bucket(object: Object): Aggregate

Description: Aggregation stage. Divides input records into different groups based on given conditions and boundaries, with each group being a bucket.

Aggregate.bucketAuto(object: Object): Aggregate

Description: Aggregation stage. Divides input records into different groups based on given conditions, with each group being a bucket. One difference from bucket is that there is no need to specify boundaries; bucketAuto automatically attempts to distribute the records as evenly as possible into each group.

Aggregate.count(fieldName: string): Aggregate

Description: Aggregation stage. Counts the number of records input to this stage from the previous aggregation stage and outputs a record with a specified field set to the count value.

Aggregate.end(): Promise\<Object>

Description: Aggregation stage. Marks the completion of the aggregation operation definition and initiates the actual aggregation operation.

Aggregate.geoNear(options: Object): Aggregate

Description: Aggregation stage. Outputs records sorted from nearest to farthest from a given point.

Aggregate.group(object: Object): Aggregate

Description: Aggregation stage. Groups input records by a given expression, with each output record representing a group and each record's _id serving as the key distinguishing different groups. Output records can also include accumulated values, where setting an output field to an accumulated value calculates it from the group.

Aggregate.limit(value: number): Aggregate

Description: Aggregation stage. Limits the number of records output to the next stage.

Aggregate.lookup(object: Object): Aggregate

Description: Aggregation stage. Performs a table join. Executes a left outer join with a specified collection in the same database. For each input record in this stage, lookup adds an array field containing a list of matching records from the joined collection. lookup then outputs the joined results to the next stage.

Aggregate.match(object: Object): Aggregate

Description: Aggregation stage. Filters documents based on conditions and passes those that meet the conditions to the next pipeline stage.

Aggregate.project(object: Object): Aggregate

Description: Aggregation stage. Passes specified fields to the next pipeline. These fields can be either existing fields or newly computed fields.

Aggregate.replaceRoot(object: Object): Aggregate

Description: Aggregation stage. Specifies an existing field as the root node of the output, or specifies a newly computed field as the root node.

Aggregate.sample(size: number): Aggregate

Description: Aggregation stage. Randomly selects a specified number of records from the documents.

Aggregate.skip(value: number): Aggregate

Description: Aggregation stage. Specifies a positive integer, skips the corresponding number of documents, and outputs the remaining documents.

Aggregate.sort(object: Object): Aggregate

Description: Aggregation stage. Sorts the input documents based on the specified fields.

Aggregate.sortByCount(object: Object): Aggregate

Description: Aggregation stage. Groups the input collection by the given expression (group). Then calculates the count of distinct groups, sorts these groups by their counts, and returns the sorted results.

Aggregate.unwind(value: string|object): Aggregate

Description: Aggregation stage. Splits documents using each element in the specified array field. After splitting, a single document becomes one or more documents, each corresponding to an element in the array.

Aggregate.bucket(object: Object): Aggregate

Description: Aggregation stage. Divides input records into different groups based on given conditions and boundaries, with each group being a bucket.

Aggregate.bucketAuto(object: Object): Aggregate

Description: Aggregation stage. Divides input records into different groups based on given conditions, with each group being a bucket. One difference from bucket is that there is no need to specify boundaries; bucketAuto automatically attempts to distribute the records as evenly as possible into each group.

Aggregate.count(fieldName: string): Aggregate

Description: Aggregation stage. Counts the number of records input to this stage from the previous aggregation stage and outputs a record with a specified field set to the count value.

Aggregate.end(): Promise\<Object>

Description: Marks the completion of the aggregation operation definition and initiates the actual aggregation operation.

Aggregate.geoNear(options: Object): Aggregate

Description: Aggregation stage. Outputs records sorted from nearest to farthest from a given point.

Aggregate.group(object: Object): Aggregate

Description: Aggregation stage. Groups input records by a given expression, with each output record representing a group and each record's _id serving as the key distinguishing different groups. Output records can also include accumulated values, where setting an output field to an accumulated value calculates it from the group.

Aggregate.limit(value: number): Aggregate

Description: Aggregation stage. Limits the number of records output to the next stage.

Aggregate.lookup(object: Object): Aggregate

Description: Aggregation stage. Performs a table join by executing a left outer join with a specified collection in the same database. For each input record in this stage, lookup adds an array field containing matching records from the joined collection that satisfy the specified conditions. lookup then outputs the joined results to the next stage.

Aggregate.match(object: Object): Aggregate

Description: Aggregation stage. Filters documents based on conditions and passes those that meet the conditions to the next pipeline stage.

Aggregate.project(object: Object): Aggregate

Description: Aggregation stage. Passes specified fields to the next pipeline. These fields can be either existing fields or newly computed fields.

Aggregate.replaceRoot(object: Object): Aggregate

Description: Aggregation stage. Specifies an existing field as the root node of the output, or specifies a newly computed field as the root node.

Aggregate.sample(size: number): Aggregate

Description: Aggregation stage. Randomly selects a specified number of records from the documents.

Aggregate.skip(value: number): Aggregate

Description: Aggregation stage. Specifies a positive integer, skips the corresponding number of documents, and outputs the remaining documents.

Aggregate.sort(object: Object): Aggregate

Description: Aggregation stage. Sorts the input documents based on the specified fields.

Aggregate.sortByCount(object: Object): Aggregate

Description: Aggregation stage. Groups the input collection by the given expression (group). Then calculates the count of distinct groups, sorts these groups by their counts, and returns the sorted results.

Aggregate.unwind(value: string|object): Aggregate

Description: Aggregation stage. Splits documents using each element in the specified array field. After splitting, a single document becomes one or more documents, each corresponding to an element in the array.