Understanding MongoDB Aggregation
MongoDB's aggregation framework enables advanced data processing operations such as calculations, transformations, and statistical analysis. It provides functionality similar to SQL's GROUP BY and aggregate functions.
The aggregate() Method
The primary method for performing aggregation operations in MongoDB is aggregate().
Basic Syntax
db.collection.aggregate(pipeline)
Practical Example
Consider a collection containing docmuent data:
{
_id: ObjectId("507f191e810c19729de860ea"),
heading: 'Database Systems Overview',
content: 'Overview of database management systems',
author: 'techguide.org',
link: 'https://www.techguide.org',
categories: ['databases', 'systems', 'NoSQL'],
views: 150
},
{
_id: ObjectId("507f191e810c19729de860eb"),
heading: 'NoSQL Databases',
content: 'Introduction to NoSQL database technologies',
author: 'techguide.org',
link: 'https://www.techguide.org',
categories: ['mongodb', 'database', 'NoSQL'],
views: 85
},
{
_id: ObjectId("507f191e810c19729de860ec"),
heading: 'Graph Database Fundamentals',
content: 'Understanding graph database concepts',
author: 'GraphDB Inc',
link: 'https://www.graphdb.com',
categories: ['graphdb', 'database', 'NoSQL'],
views: 420
}
To count documents per author:
db.documents.aggregate([
{$group: {
_id: "$author",
document_count: {$sum: 1}
}}
])
This produces results equivalent to the SQL query:
SELECT author, COUNT(*) FROM documents GROUP BY author
Aggregation Expressions
| Expression | Description | Example |
|---|---|---|
| $sum | Calculates total sum | {$sum: "$views"} |
| $avg | Computes average value | {$avg: "$views"} |
| $min | Finds minimum value | {$min: "$views"} |
| $max | Finds maximum value | {$max: "$views"} |
| $push | Adds values to array (allows duplicates) | {$push: "$link"} |
| $addToSet | Adds unique values to array | {$addToSet: "$link"} |
| $first | Returns first document value | {$first: "$link"} |
| $last | Returns last document value | {$last: "$link"} |
Pipeline Processing Concept
MongoDB aggregation pipelines process documents through multiple stages, with each stage's output becoming the next stage's input. Common pipeline operators include:
- $project: Reshapes documents, includes/excludes fields
- $match: Filters documents based on criteria
- $limit: Restricts number of output documents
- $skip: Omits specified number of documents
- $unwind: Deconstructs array fields
- $group: Groups documents for aggregation
- $sort: Orders output documents
- $geoNear: Returns documents based on geographic proximity
Pipeline Operator Examples
Field projection example:
db.articles.aggregate([
{$project: {
_id: 0,
heading: 1,
author: 1
}}
])
Filtering and grouping example:
db.articles.aggregate([
{$match: {views: {$gt: 100}}},
{$group: {_id: "$author", total_views: {$sum: "$views"}}}
])
Note that pipeilne stage order significantly affects results. Filter stages should typical precede grouping stages.
Date Aggregation Operations
MongoDB provides date-specific aggregation operators for temporal analysis:
$dayOfYear, $dayOfMonth, $dayOfWeek
$year, $month, $week
$hour, $minute, $second, $millisecond
$dateToString
Example of daily aggregation:
db.event_log.aggregate([
{$match: {
user_id: 10001,
event_time: {$gt: ISODate("2023-01-01")}
}},
{$group: {
_id: {$dayOfMonth: "$event_time"},
event_count: {$sum: 1}
}},
{$sort: {"_id": 1}}
])