Professional Documents
Culture Documents
018 DBMS Practical No 7
018 DBMS Practical No 7
CNUM: UCE2022018
Name:Vaishnavi Gaikwad
Problem Statement:
Solve the given queries using the aggregate() and mapReduce() functions
in MongoDB
Q1 Consider the student collection and solve the following queries using the aggregate() function.
OUTPUT:
{ "_id" : ObjectId("644e6311f7949f405393ffb7"), "name" : "Shilpi", "class" : "SY Comp", "div" : "C",
"rollno" : 2932, "dept" : "COMPUTER", "semester" : 3, "address" : { "City" : "Kolhapur" },
"percentage" : 72, "sport" : "Chess", "Phno" : 9850436611 }
{ "_id" : ObjectId("644e6311f7949f405393ffb8"), "name" : "Shriya", "class" : "SY Comp", "div" : "C",
"rollno" : 2022, "dept" : "COMPUTER", "semester" : 3, "address" : { "City" : "pune" }, "percentage" :
92, "sport" : "Hockey", "Phno" : 9150436611 }
{ "_id" : ObjectId("644e6311f7949f405393ffb9"), "name" : "Samiksha", "class" : "SY Comp", "div" :
"C", "rollno" : 2017, "dept" : "COMPUTER", "semester" : 3, "address" : { "City" : "Kothrud" },
"percentage" : 90, "sport" : "Football", "Phno" : 8950436611 }
{ "_id" : ObjectId("644e6311f7949f405393ffba"), "name" : "Vaishnavi", "class" : "SY Comp", "div" :
"C", "rollno" : 2018, "dept" : "COMPUTER", "semester" : 3, "address" : { "City" : "Karvenagar" },
"percentage" : 91, "sport" : "Cricket", "Phno" : 9850676611 }
{ "_id" : ObjectId("644e6311f7949f405393ffbb"), "name" : "diksha", "class" : "TE Comp", "div" : "A",
"rollno" : 2010, "dept" : "Computer", "semester" : 5, "address" : { "City" : "Satara" }, "percentage" : 55,
"sport" : "Chess", "Phno" : 9850436345 }
{ "_id" : ObjectId("644e6311f7949f405393ffbc"), "name" : "Diya", "class" : "SY Comp", "div" : "C",
"rollno" : 2832, "dept" : "ENTC", "semester" : 3, "address" : { "City" : "Mumbai" }, "percentage" : 58,
"sport" : "Chess", "Phno" : 9850436611 }
{ "_id" : ObjectId("644e6311f7949f405393ffbd"), "name" : "kavya", "class" : "SY", "div" : "TY",
"rollno" : 1232, "dept" : "Intru", "semester" : 3, "address" : { "City" : "Karad" }, "percentage" : 65,
"sport" : "cricket", "Phno" : 9850438976 }
{ "_id" : ObjectId("644e6311f7949f405393ffbe"), "name" : "Harsha", "class" : "SY", "div" : "C",
"rollno" : 2032, "dept" : "MECH", "semester" : 3, "address" : { "City" : "Kolhapur" }, "percentage" : 72,
"sport" : "Chess", "Phno" : 82350436611 }
{ "_id" : ObjectId("644e6311f7949f405393ffbf"), "name" : "Siya", "class" : "TE Comp", "div" : "A",
"rollno" : 2930, "dept" : "COMPUTER", "semester" : 5, "address" : { "City" : "Kolhapur" },
"percentage" : 82, "sport" : "Hockey", "Phno" : 8850436611 }
{ "_id" : ObjectId("644e6311f7949f405393ffc0"), "name" : "Riya", "class" : "SY", "div" : "A", "rollno"
: 1932, "dept" : "ENTC", "semester" : 3, "address" : { "City" : "Satara" }, "percentage" : 62, "sport" :
"Football", "Phno" : 9850433498 }
{ "_id" : ObjectId("644e6311f7949f405393ffc1"), "name" : "vidya", "class" : "TE Comp", "div" : "A",
"rollno" : 1930, "dept" : "COMPUTER", "semester" : 1, "address" : { "City" : "pune" }, "percentage" :
72, "sport" : "Cricket", "Phno" : 9850436610 }
{ "_id" : ObjectId("644e6311f7949f405393ffc2"), "name" : "Shrija", "class" : "TY Comp", "div" : "A",
"rollno" : 1032, "dept" : "COMPUTER", "semester" : 1, "address" : { "City" : "Kolhapur" },
"percentage" : 57, "sport" : "Chess", "Phno" : 8750436611 }
{ "_id" : ObjectId("644e6311f7949f405393ffc3"), "name" : "Shilpa", "class" : "SY", "rollno" : 2932,
"dept" : "MECH", "semester" : 3, "address" : { "City" : "Lonavla" }, "percentage" : 62, "sport" :
"Carrom", "Phno" : 9876436611 }
{ "_id" : ObjectId("644e6311f7949f405393ffc4"), "name" : "Sanika", "class" : "SY Comp", "div" : "C",
"rollno" : 2932, "dept" : "COMPUTER", "semester" : 1, "address" : { "City" : "Kolhapur" },
"percentage" : 82, "sport" : "Chess", "Phno" : 9850436611 }
Q1 Consider the student collection and solve the following queries using the aggregate()
function. {name:"Shilpi",class:"SY Comp", div: "C", rollno: 2932, dept:"COMPUTER",
semester:1, address: {City: "Kolhapur"}, percentage: 72, sport: "Chess", Phno: 9850436611} 1.
Display list of all students of TYComp A
library2018> db.student1.aggregate([{$match :{class:"TY Comp", div :"A"}}]);
Output:
[
{
_id: ObjectId("644ea5a9267865518d1705fc"),
name: 'Shrija',
class: 'TY Comp',
div: 'A',
rollno: 1032,
dept: 'COMPUTER',
semester: 1,
address: { City: 'Kolhapur' },
percentage: 57,
sport: 'Chess',
Phno: 8750436611
}
]
library2018> db.student1.aggregate([{$match :{class:"TE Comp", div :"A"}}]);
Output:
[
{
_id: ObjectId("644ea53e267865518d1705f5"),
name: 'diksha',
class: 'TE Comp',
div: 'A',
rollno: 2010,
dept: 'Computer',
semester: 5,
address: { City: 'Satara' },
percentage: 55,
sport: 'Chess',
Phno: 9850436345
},
{
_id: ObjectId("644ea577267865518d1705f9"),
name: 'Siya',
class: 'TE Comp',
div: 'A',
rollno: 2930,
dept: 'COMPUTER',
semester: 5,
address: { City: 'Kolhapur' },
percentage: 82,
sport: 'Hockey',
Phno: 8850436611
},
{
_id: ObjectId("644ea58d267865518d1705fb"),
name: 'vidya',
class: 'TE Comp',
div: 'A',
rollno: 1930,
dept: 'COMPUTER',
semester: 1,
address: { City: 'pune' },
percentage: 72,
sport: 'Cricket',
Phno: 9850436610
}
]
2. Display RollCall of TYComp A (Show only RollNo and Names)
library2018> db.student1.aggregate([{$match:{class:"TE Comp" ,div:"A"}},
{$project:{rollno:1,name:1,_id:0}}]);
OUTPUT:
[
{ name: 'diksha', rollno: 2010 },
{ name: 'Siya', rollno: 2930 },
{ name: 'vidya', rollno: 1930 }
]
[
{
_id: ObjectId("644ea577267865518d1705f9"),
name: 'Siya',
class: 'TE Comp',
div: 'A',
rollno: 2930,
dept: 'COMPUTER',
semester: 5,
address: { City: 'Kolhapur' },
percentage: 82,
sport: 'Hockey',
Phno: 8850436611
},
{
_id: ObjectId("644ea58d267865518d1705fb"),
name: 'vidya',
class: 'TE Comp',
div: 'A',
rollno: 1930,
dept: 'COMPUTER',
semester: 1,
address: { City: 'pune' },
percentage: 72,
sport: 'Cricket',
Phno: 9850436610
}
]4. Display name class and marks of all stud
db.student1.aggregate([{$project:{name:1,class:1,percentage:1,_id:0}}]);
OUTPUT:
{ "name" : "Shilpi", "class" : "SY Comp", "percentage" : 72 }
{ "name" : "Shriya", "class" : "SY Comp", "percentage" : 92 }
{ "name" : "Samiksha", "class" : "SY Comp", "percentage" : 90 }
{ "name" : "Vaishnavi", "class" : "SY Comp", "percentage" : 91 }
{ "name" : "diksha", "class" : "TE Comp", "percentage" : 55 }
{ "name" : "Diya", "class" : "SY Comp", "percentage" : 58 }
{ "name" : "kavya", "class" : "SY", "percentage" : 65 }
{ "name" : "Harsha", "class" : "SY", "percentage" : 72 }
{ "name" : "Siya", "class" : "TE Comp", "percentage" : 82 }
{ "name" : "Riya", "class" : "SY", "percentage" : 62 }
{ "name" : "vidya", "class" : "TE Comp", "percentage" : 72 }
{ "name" : "Shrija", "class" : "TY Comp", "percentage" : 57 }
{ "name" : "Shilpa", "class" : "SY", "percentage" : 62 }
{ "name" : "Sanika", "class" : "SY Comp", "percentage" : 82 }
{ "name" : "Shilpi", "class" : "SY Comp", "percentage" : 72 }
{ "name" : "Shriya", "class" : "SY Comp", "percentage" : 92 }
{ "name" : "Samiksha", "class" : "TY Comp", "percentage" : 90 }
{ "name" : "Vaishnavi", "class" : "SY Comp", "percentage" : 91 }
{ "name" : "diksha", "class" : "TE Comp", "percentage" : 55 }
{ "name" : "Diya", "class" : "SY Comp", "percentage" : 58 }
[
{ name: 'diksha', class: 'TE Comp', percentage: 55 },
{ name: 'Siya', class: 'TE Comp', percentage: 82 },
{ name: 'vidya', class: 'TE Comp', percentage: 72 }
]
6. Display name class and marks of all stud of TEComp A in descending order of marks
db.student1.aggregate([{$match:{class:"TE
Comp",div:"A"}},{$project:{name:1,class:1,percentage:1,_id:0}},{$sort:{percentage:-1}}]);
OUTPUT:
{ "name" : "Siya", "class" : "TE Comp", "percentage" : 82 }
{ "name" : "vidya", "class" : "TE Comp", "percentage" : 72 }
{ "name" : "diksha", "class" : "TE Comp", "percentage" : 55 }
library2018> db.student1.aggregate([{$group:{_id:"$class",count:{$sum:1}}}]);
OUTPUT:
[
{ _id: 'TE Comp', count: 3 },
{ _id: 'SY Comp', count: 6 },
{ _id: 'TY', count: 1 },
{ _id: 'SY', count: 4 },
{ _id: 'TY Comp', count: 1 }
]
9. Show the count of number of students in each division along with average marks
library2018>
db.student1.aggregate([{$group:{_id:"$div",AverageMarks:{$avg:"$percentage"},count:{$sum:1
}}}]);
OUTPUT:
[
{ _id: 'C', AverageMarks: 79.57142857142857, count: 7 },
{ _id: 'A', AverageMarks: 68.83333333333333, count: 6 },
{ _id: 'TY', AverageMarks: 65, count: 1 },
{ _id: null, AverageMarks: 62, count: 1 }
]
10. Display name and marks of topper of each div
library2018>
db.student1.aggregate([{$group:{_id:"$div",name:{$first:"$name"},Topper:{$max:"$perc
entage"}}}]);
OUTPUT
[
{ _id: null, name: 'Shilpa', Topper: 62 },
{ _id: 'A', name: 'Sakshi', Topper: 85 },
{ _id: 'TY', name: 'kavya', Topper: 65 },
{ _id: 'C', name: 'Shilpi', Topper: 92 }
]
11. Display name and marks of topper of each div and show in sorted order
library2018>
db.student1.aggregate([{$group:{_id:"$div",name:{$first:"$name"},Topper:{$max:"$perc
entage"}}},{$sort:{Topper:-1}}]).pretty();
OUTPUT:
[
{ _id: 'C', name: 'Shilpi', Topper: 92 },
{ _id: 'A', name: 'Sakshi', Topper: 85 },
{ _id: 'TY', name: 'kavya', Topper: 65 },
{ _id: null, name: 'Shilpa', Topper: 62 }
]
12. Display the first 5 toppers of TY
library2018> db.student1.aggregate([{$match:{class:"TE
Comp"}},{$sort:{percentage:1}},{$limit:5},{$project:{"_id":0,"name":1,"class":1,"div":1,"perc
entage":1}}]);
Output:
[
{ name: 'diksha', class: 'TE Comp', div: 'A', percentage: 55 },
{ name: 'vidya', class: 'TE Comp', div: 'A', percentage: 72 },
{ name: 'Siya', class: 'TE Comp', div: 'A', percentage: 82 }
Q2 Create index on the name field and use the explain() function to understand the
query execution plan and note the performance enhancement
library2018> db.student1.getIndexes();
Output :
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{ v: 2, key: { rollno: 1 }, name: 'rollno_1' }
]
2) Write a mapreduce query for finding the count of each profession in the customer collection
3.Write a mapreduce query for finding the count of fields in each document in the customer
collection.
library2018> db.customers.mapReduce(map,reduce,{out:"Output"})
{ result: 'Output', ok: 1 }
library2018> db.Output.find()
[
{ _id: 'Samir', value: 7 },
{ _id: 'Om', value: 7 },
{ _id: 'sheetal', value: 7 },
{ _id: 'Sanika', value: 7 },
{ _id: 'Suresh', value: 7 },
{ _id: 'Sakshi', value: 7 },
{ _id: 'Sudir', value: 7 },
{ _id: 'Pratham', value: 7 },
{ _id: 'Vaibhavi', value: 7 },
{ _id: 'Swara', value: 7 },
{ _id: 'Harshada', value: 7 },
4) Write a mapreduce query for finding the total amount spent by each customer till now.
library2018> db.customers.mapReduce(mapper,reducer,{out:"Output"})
DeprecationWarning: Collection.mapReduce() is deprecated. Use an aggregation instead.
See https://docs.mongodb.com/manual/core/map-reduce for details. { result: 'Output',
ok: 1 }
library2018> db.Output.find()
[
{ _id: 'Samir', value: 2550 },
{ _id: 'Samiksha', value: 2552 },
{ _id: 'Om', value: 2220 },
{ _id: 'sheetal', value: 1450 },
{ _id: 'Harshada', value: 1250 },
{ _id: 'Pratham', value: 1250 },
{ _id: 'Sudir', value: 1550 },
{ _id: 'Sakshi', value: 1250 },
{ _id: 'Suresh', value: 2550 },
{ _id: 'Vaibhavi', value: 550 },
{ _id: 'Sanika', value: 2250 },
{ _id: 'Swara', value: 1550 }
]