Description: Aggregate functions in NoSQL databases are used to perform calculations on a set of values, returning a single value.
1. COUNT
- Description: Counts the number of items in a collection or those that match a certain condition.
- Example (MongoDB) :
db.collection.count({status: "active"});
Counts the number of documents where the status is “active”.
2. SUM
- Description: Adds together all the numerical values found in a specified field across a collection.
- Example (MongoDB) :
db.sales.aggregate([{$group: {_id: null, totalSales: {$sum: "$amount"}}}]);
Sums up the amount field for all documents in the sales collection.
3. AVG
- Description: Calculates the average of the numerical values in a specified field.
- Example (MongoDB) :
db.sales.aggregate([{$group: {_id: null, averageSale: {$avg: "$amount"}}}]);
Computes the average of the amount field across all documents.
4. MAX
- Description: Returns the maximum value from the specified field.
- Example (MongoDB) :
db.sales.aggregate([{$group: {_id: null, maxSale: {$max: "$amount"}}}]);
Finds the maximum amount in the sales collection.
5. MIN
- Description: Returns the minimum value from the specified field.
- Example (MongoDB) :
db.sales.aggregate([{$group: {_id: null, minSale: {$min: "$amount"}}}]);
Finds the minimum amount in the sales collection.