MongoDB Aggregation framework

MongoDB Aggregation framework

The MongoDB aggregation framework is an other way to query our data, it gives us more power over our queries (group by, sum, unwind, lookup) and can perform more queries than a simple find method. The aggregation framework is a pipeline of queries that runs one after an other based on the result of the previous stage to gives us at the end a single result. Aggregation operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods. In this tutorial we are going to take a look at The aggragation pipeline.

Your first aggregation operation

This is just an example of aggregation pipeline. We'll dive into the details later in this article.

Inserting some data in MongoDB

db.articles.insert([
{author: "yamicode", name: "article 1", date: new Date("02/12/207"), views: 10},
{author: "yamicode", name: "article 2", date: new Date("02/12/207"), views: 5},
{author: "jhon", name: "article 3", date: new Date("02/12/207"), views: 12},
])

Performing a match stage in MongoDB to find articles from a single author and counting the number of articles

db.articles.aggregate([
{
	$match:{
		"author": "yamicode"
	}
},
{
	$group:{
		_id: null,
		articles: { $sum: 1 }
	}
}
])
Result
{ "_id" : null, "articles" : 2 }

MongoDB Aggregation framework in details

The aggregation method takes an array of operation grouped as stages. In this table we'll list some of them:

Aggregation Pipeline Stages

Here a list of some usefull pipeline stages that you'll need to know and are used often

Operation Role Example
$match Filters the documents {$match: {"author": "yamicode", vues: {$gt: 5} }}
$project Passes along the documents with the requested fields to the next stage in the pipeline {$project: {"author": 1, "author-article": {$concat: ["$author", "-", "$name"]}}}
$sort Sorts all input documents {$sort: {"author": 1, "views": -1}}
$limit Limit the number of documents {$limit: 5}
$skip Skips over the specified number of documents {$skip: 5}
$group gGroups input documents by the specified _id expression and for each distinct grouping, outputs a document {$group: { _id: {author: "$author", ...}, nbr: {$sum: "$views"} }}
$unwind Deconstructs an array field from the input documents to output a document for each element. {$unwind: "$array"}
$lookup Performs a left outer join to an unsharded collection in the same database $lookup: { from: "users", localField: "user_id", foreignField: "id", as: "user" }
$count Count of the number of documents {$count: "myCount"}
$addField Adds new fields to documents { $addFields: { <newField>: <expression>, ... } }
$out Takes the documents returned by the aggregation pipeline and writes them to a specified collection {$out: "<output-collection>"}
$replaceRoot Replaces the input document with the specified document { $replaceRoot: { newRoot: "$replacementDocument" } }

Aggregation Pipeline Operators

Here a list of some usefull pipeline operators that you'll need to know and are used often

Operation Role
$add, $mul, $divide, $abs, $ceil, $log Arithmetic Expression
$eq, $gt, $lt Comparison Expression Operators
$not, $or, $and Boolean Expression Operators
$year, $week, $month, $dateToString Date Expression Operators
$size Returns the number of elements in the array. Accepts a single expression as argument.
$in Returns a boolean indicating whether a specified value is in an array.
$arrayElemAt Returns the element at the specified array index.
$objectToArray Converts a document to an array of documents representing key-value pairs.
$mergeObjects Combines multiple documents into a single document.

To find more about operators visit the official documentation: Aggregation Pipeline Operators, Aggregation Pipeline Stages