Skip to main content

11 docs tagged with "aggregate"

View All Tags

$bucket

The $bucket operator in MongoDB is used within an aggregation pipeline to categorize incoming documents into buckets or groups based on a specified expression and boundaries. This operator is particularly useful for dividing a collection of documents into ranges and performing aggregate calculations on each range.

$group

The $group stage in MongoDB's aggregation pipeline is used to group documents by some specified expression and output a document for each distinct grouping. The grouped documents can then be processed using various accumulator operators to perform calculations such as summing, averaging, or finding the minimum and maximum values.

$limit

The $limit stage in MongoDB's aggregation pipeline is used to limit the number of documents passed to the next stage in the pipeline. It provides a way to sample data and can be particularly useful for pagination, testing, or optimizing performance by reducing the amount of data that has to be processed in subsequent stages.

$lookup

The $lookup stage in MongoDB performs a left outer join to another collection in the same database to filter in documents from the "joined" collection for processing. It's part of MongoDB's aggregation framework and allows you to combine documents from different collections based on a related field between them.

$match

The $match stage in MongoDB's aggregation pipeline is used for filtering documents to pass only those that meet certain conditions to the next stage in the pipeline. It's similar to the WHERE clause in SQL queries. The $match stage can significantly reduce the number of documents that have to be examined in subsequent stages of the pipeline, thereby improving performance.

$out

The $out operator is used in a MongoDB aggregation pipeline to write the result of the aggregation to a specified collection. The $out stage must be the last stage in the pipeline, and it effectively replaces the existing collection with the new one containing the aggregated results. If the specified collection does not exist, MongoDB will create it.

$project

The $project stage in MongoDB's aggregation pipeline is used to selectively pass along specified fields to the next stage of the pipeline. This stage can either add new fields, remove existing fields, or reshape and transform the structure of the documents. It's similar to the SELECT statement in SQL but with more capabilities, such as renaming fields, computing new values, and creating nested structures.

$skip

The $skip stage in MongoDB's aggregation pipeline is used to skip over a specified number of documents from the pipeline and pass the remaining documents to the next stage. This operation is often used in conjunction with $limit for pagination purposes. It can also be useful for skipping records that you don't want to include in the final output for other reasons.

$sort

The $sort stage in MongoDB's aggregation pipeline is used to sort the documents based on one or more fields. Sorting can be done in ascending or descending order, and it's often used in conjunction with other aggregation stages like $group, $match, and $project to organize the output.

$unwind

The $unwind stage in MongoDB's aggregation pipeline is used to deconstruct an array field from the input documents, generating a new document for each element in the array. Each new document replaces the original document and contains all its fields except for the array, which is replaced by an element from the array. This is particularly useful when you have an array of objects or values and you want to flatten it out for easier processing in subsequent stages.

Aggregate

In MongoDB, the aggregation framework provides a powerful and flexible way to transform and combine data in your collections. Aggregation operations process data records and return computed results, much like how you would use GROUP BY and aggregate functions like SUM, AVG, etc., in SQL.