A Brief Introduction To Aggregation In MongoDB

Posted By : Ankit Uniyal | 31-May-2018

In this blog, we will discuss aggregation in MongoDB, aggregation is an operation which processes the data and returns computed results. It group values from multiple documents and perform the variety of operations which results into the single value.

 

Three types of aggregation can be performed in MongoDB: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods.

 

Aggregation Pipeline: It is a framework for data aggregation model which allows the developer to express functional pipelines so that it can perform preparation, shaping, and analysis of data. As Document flows through multi-stage pipeline and results into aggregated results.

// $match all celestial bodies, not equal to Star
db.solarSystem.aggregate([{
    "$match": {
        "type": {
            "$ne": "Star"
        }
    }
}]).pretty()

// same query using find command
db.solarSystem.find({
    "type": {
        "$ne": "Star"
    }
}).pretty();

// count the number of matching documents
db.solarSystem.count();

// using $count
db.solarSystem.aggregate([{
    "$match": {
        "type": {
            "$ne": "Star"
        }
    }
}, {
    "$count": "planets"
}]);

// matching on value, and removing ``_id`` from projected document
db.solarSystem.find({
    "name": "Earth"
}, {
    "_id": 0
});    
        
Map-Reduce: Aggregation can be also performed using Map-Reduce operations in MongoDB.It goes through two phases first map stage and second reduce. In general, map stage process each document and return one more object for every input document. And reduce phases combines the result of map stage and return the single document.

db.solarSystem.mapReduce(
    function() {
        emit(key, value);
    }, // map function
    function(key, values) {
        return reduceFunction
    }, // reduce function
    {
        out: collection
    }
)    
        
Single Purpose Aggregation Methods: As its name implies, single-purpose aggregation method used to do a specific operation on a set of data as when we need to perform only one operation then it called as Single Purpose aggregation method. These methods are less complex but have the limited scope of we compare this with pipeline and nap-reduce operation.

 

Below are some of the single purpose aggregation operations :

 

1. Count: It counts all the documents or the number of the documents which match a specific query.

 

db.employees.count({ joining_date: { $gte: new Date("2018","06","28") }})       
        


2. Distinct: Operation which returns only distinct values from a key in a collection.

db.companies.distinct("emp_dept")     
        


Thanks

About Author

Author Image
Ankit Uniyal

Ankit has knowledge in Javascript, NodeJS, AngularJS and MongoDB also have experience in using AWS Services.

Request for Proposal

Name is required

Comment is required

Sending message..