MongoDB Aggregation
MongoDB Aggregation
$match stage – filters those documents we need to work with, those that fit our needs
$group stage – does the aggregation job
$sort stage – sorts the resulting documents the way we require (ascending or
descending)
use employeedatabase
db.employee.insert([
{"name":"Employee1", "salary":40000},
{"name":"Employee2", "salary":30000},
{"name":"Employee3", "salary":50000}
]);
Output:
[
{"name":"Employee1", "salary":40000},
{"name":"Employee2", "salary":30000},
{"name":"Employee3", "salary":50000}
]
db.employee.aggregate([{$skip:1}]); or
db.employee.aggregate({$skip:1});
Output:
[
{"_id":2, "name":"Employee2", "salary":30000},
{"_id":3, "name":"Employee3", "salary":50000}
]
Output:
[
{"_id":"3", name:"Employee3", salary:50000},
Explanation:- The 1 is used for ascending Order and -1 for descending Order. It
sorts documents by salary column name in employee collection.
Explanation:- It gets sum of total number of employees on the basis of salary column.
In this example, salary column of employee collection is used to calculate some of all
employees in the collection.
Output:
[
{"_id":"Employee1", "Total No. Of Employees":1},
{"_id":"Employee2", "Total No. Of Employees":1},
{"_id":"Employee3", "Total No. Of Employees":1}
]
db.student.aggregate([{$unwind:"$courses"}]); or
db.student.aggregate({$unwind:"$courses"});
Output:
[
{_id:10, name:"Employee4", salary:50000, course: "Java"},
{_id:10, name:"Employee4", salary:50000, course: "JavaScript"},
{_id:10, name:"Employee4", salary:50000, course: "MySQL"}
]
student collection:-
[
{_id:10, name:"Employee 1", student_id:1},
{_id:11, name:"Employee 2", student_id:2},
{_id:12, name:"Employee 3", student_id:3}
]
student_address collection:-
[
{_id:20, address:"Address 1", student_id:1},
{_id:21, address:"Address 1", student_id:1},
{_id:22, address:"Address 2", student_id:2},
{_id:23, address:"Address 2", student_id:2},
{_id:24, address:"Address 3", student_id:3},
{_id:25, address:"Address 3", student_id:3}
]
db.student.aggregate([
{
$lookup:{
localField:"student_id",
from:"student_address",
foreignField:"student_id",
as:"student_info"
}
}
]);
Output:
[
{
_id:10,
name:"Employee 1",
student_id:1,
student_info:[
{_id:20, address:"Address 1",
student_id:1},
{_id:21, address:"Address 1",
student_id:1}
]
},
{
_id:11,
name:"Employee 2",
student_id:2,
student_info:[
{_id:22, address:"Address 2",
student_id:2},
{_id:23, address:"Address 2",
student_id:2}
]
},
{
_id:12,
name:"Employee 3",
student_id:3,
student_info:[
{_id:24, address:"Address 3",
student_id:3},
{_id:25, address:"Address 3",
student_id:3}
]
}
]
Explanation1:- The $lookup aggregate function takes a json object which includes
many properties.
localField: This is the id from current collection that would be used to lookup
documents. In this example that is student_id.
from: This is the another collection with whcih we want ot perform join operation. In
this example this is student_address.
foriegnField: This is the id from another collection which is used to perform join
operation between/among collection(s).
as: This is the name given to matched number of documents after joining operations.
In this example, student_info would be given name.