MongoDB Aggregate Exercise2
MongoDB Aggregate Exercise2
1. Display a list stating how many tutorials are written by each user.
db.tutorial.aggregate({$group:{_id:"$by_user",tutorial:
{$push:"$title"},tutorial_count:{$sum:1}}})
2. Calculate and display the total likes, average likes, minimum and maximum likes
Pipeline Concept
In UNIX command, shell pipeline means the possibility to execute an
operation on some input and use the output as the input for the next
command and so on. MongoDB also supports same concept in aggregation
framework. There is a set of possible stages and each of those is taken as a
set of documents as an input and produces a resulting set of documents (or
the final resulting JSON document at the end of the pipeline). This can then
in turn be used for the next stage and so on.
$match − This is a filtering operation and thus this can reduce the amount of
documents that are given as input to the next stage.
$skip − With this, it is possible to skip forward in the list of documents for a
given amount of documents.
$limit − This limits the amount of documents to look at, by the given
number starting from the current positions.
$unwind − This is used to unwind document that are using arrays. When using
an array, the data is kind of pre-joined and this operation will be undone with
this to have individual documents again. Thus with this stage we will increase
the amount of documents for the next stage.
Data in JSON format, shows the hosting provider for website.
website.json
{ "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }
db.hosting.aggregate({$group:{_id:"$hosting",total_documents:{$sum:1}}})
5. Display the total number of documents for each hosting in descending order of
total number.
db.hosting.aggregate({$group:{_id:"$hosting",total_documents:
{$sum:1}}},{$sort:{"total_documents":-1}})
db.hosting.aggregate({$group:{_id:"$hosting",total_documents:{$sum:1}}},
{$sort:{"total_documents":-1}},{$limit:3})
References
1. MongoDB Aggregation
2. MongoDB db.collection.aggregate()
3. Aggregation Pipeline Limits
4. MongoDB Hello World Example
{
"id": "1",
"firstName": "Jane",
"lastName": "Doe",
"phoneNumber": "555-555-1212",
"city": "Beverly Hills",
"state: "CA",
"zip": 90210
"email": "Jane.Doe@compose.io"
}
8. Find all users that live in the area with 90210 area code.
db.area.aggregate([{$match:{"zip":90210}},{$project:
{"zip":1,"firstName":1}}]);
9. Find the number of users that live in the area with 90212 area code.
db.area.aggregate([{$match:{"zip":90212}},{$group:{"_id":"$zip","count":
{$sum:1}}}]);
10. Find the total number of users.
db.area.aggregate([{$group:{"_id":"$firstName","user_count":{$sum:1}}}]);
db.area.aggregate([{$match:{"zip":90210}}]);
12. Retrieve First name, city, zip code and email of the documents whose state is
“CA”
db.area.aggregate([{$match:{"state":"CA"}},{$project:
{firstName:1,city:1,zip:1,email:1}}]);
db.area.aggregate([{$match:{"zip":90210}},{$group:{"_id":"$zip","count":
{$sum:1}}}]);
Our transaction model looks like this:
"id": "1",
"productId": "1",
"customerId": "1",
"amount": 20.00,
"transactionDate": ISODate("2017-02-23T15:25:56.314Z")
}
9. Calculate the total amount of sales made for the month of January.
Now suppose we have a School database and have a Student Collection as below
db.Student.insert({StudentName : "Vijay",Section :
"A",Marks:70,Subject:["Hindi","English","Math"]})
db.Student.insert({StudentName : "Gaurav",Section : "A",Marks:90,Subject:["English"]})
db.Student.insert({StudentName : "Ajay",Section : "A",Marks:70,Subject:["Math"]})
db.Student.insert({StudentName : "Ankur",Section : "B",Marks:10,Subject:["Hindi"]})
db.Student.insert({StudentName : "Sunil",Section : "B",Marks:70,Subject:["Math"]})
db.Student.insert({StudentName : "Preeti",Section : "C",Marks:80,Subject:["Hindi","English"]})
db.Student.insert({StudentName : "Anuj",Section : "C",Marks:50,Subject:["English"]})
db.Student.insert({StudentName : "Palka",Section : "D",Marks:40,Subject:["Math"]})
db.Student.insert({StudentName : "Soniya",Section : "D",Marks:20,Subject:["English","Math"]})
db.Student.insert({StudentName : "Tarun",Section : "A",Marks:95,Subject:[]})
db.Student.insert({StudentName : "Saurabh",Section : "A",Marks:95})
1. Find out all the documents where Section is A and Marks is greater then 80/
2. Findout StudentName,ection and Marks from Student Collectionwhere Section is 'A'.
3. Display a document for each subject for the student ‘Vijay’ only the fields
StudentName,Marks,Subject
4. Include the array index in the previous query.
5. Findout Total Marks group by Section.
6. Find out Total Marks for Section A
7. Fetchthe count of students in each section and Total marks and average marks as well
8. Renamethe column Names in above query(Section to SectionName and TotalMarls to Total)
i) by Section name
ii) by descending order of total marks