Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

ASSIGNMENT-7

CNUM: UCE2022018
Name:Vaishnavi Gaikwad

Aim: To implement MongoDB Aggregation

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.

INSERTING OF RECORD IN THE DATABASE


use library2018;

db.student1.insertOne({name:"Shilpi",class:"SY Comp", div: "C", rollno: 2932, dept:"COMPUTER",


semester:3,
address: {City: "Kolhapur"}, percentage: 72, sport: "Chess", Phno: 9850436611});

db.student1.insertOne({name:"Shriya",class:"SY Comp", div: "C", rollno: 2022, dept:"COMPUTER",


semester:3,
address: {City: "pune"}, percentage: 92, sport: "Hockey", Phno: 9150436611});

db.student1.insertOne({name:"Samiksha",class:"SY Comp", div: "C", rollno: 2017,


dept:"COMPUTER", semester:3,
address: {City: "Kothrud"}, percentage: 90, sport: "Football", Phno: 8950436611});

db.student1.insertOne({name:"Vaishnavi",class:"SY Comp", div: "C", rollno: 2018,


dept:"COMPUTER", semester:3,
address: {City: "Karvenagar"}, percentage: 91, sport: "Cricket", Phno: 9850676611});

db.student1.insertOne({name:"Sakshi",class:"TY", div: "A", rollno: 2032, dept:"COMPUTER",


semester:5,
address: {City: "Kolhapur"}, percentage: 85, sport: "Hockey", Phno: 9870436611});

db.student1.insertOne({name:"diksha",class:"TE Comp", div: "A", rollno: 2010, dept:"Computer",


semester:5,
address: {City: "Satara"}, percentage: 55, sport: "Chess", Phno: 9850436345});

db.student1.insertOne({name:"Diya",class:"SY Comp", div: "C", rollno: 2832, dept:"ENTC",


semester:3,
address: {City: "Mumbai"}, percentage: 58, sport: "Chess", Phno: 9850436611});
db.student1.insertOne({name:"kavya",class:"SY", div: "TY", rollno: 1232, dept:"Intru", semester:3,

address: {City: "Karad"}, percentage: 65, sport: "cricket", Phno: 9850438976});

db.student1.insertOne({name:"Harsha",class:"SY", div: "C", rollno: 2032, dept:"MECH", semester:3,


address: {City: "Kolhapur"}, percentage: 72, sport: "Chess", Phno: 82350436611});

db.student1.insertOne({name:"Siya",class:"TE Comp", div: "A", rollno: 2930, dept:"COMPUTER",


semester:5,
address: {City: "Kolhapur"}, percentage: 82, sport: "Hockey", Phno: 8850436611});

db.student1.insertOne({name:"Riya",class:"SY", div: "A", rollno: 1932, dept:"ENTC", semester:3,


address: {City: "Satara"}, percentage: 62, sport: "Football", Phno: 9850433498});

db.student1.insertOne({name:"vidya",class:"TE Comp", div: "A", rollno: 1930, dept:"COMPUTER",


semester:1,
address: {City: "pune"}, percentage: 72, sport: "Cricket", Phno: 9850436610});

db.student1.insertOne({name:"Shrija",class:"TY Comp", div: "A", rollno: 1032, dept:"COMPUTER",


semester:1,
address: {City: "Kolhapur"}, percentage: 57, sport: "Chess", Phno: 8750436611});

db.student1.insertOne({name:"Shilpa",class:"SY", rollno: 2932, dept:"MECH", semester:3,


address: {City: "Lonavla"}, percentage: 62, sport: "Carrom", Phno: 9876436611});

db.student1.insertOne({name:"Sanika",class:"SY Comp", div: "C", rollno: 2932, dept:"COMPUTER",


semester:1,
address: {City: "Kolhapur"}, percentage:82, sport: "Chess", Phno:
9850436611}); db.student1.find();

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 }
]

3. Display list of all stud of TYComp A having marks >60


library2018> db.student1.aggregate([{$match:{class:"TE
Comp",div:"A",percentage:{$gt:60}}}]);
Output:

[
{
_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 }

5. Display name class and marks of all stud of TE Comp A


library2018> db.student1.aggregate([{$match:{class:"TE
Comp",div:"A"}},{$project:{name:1,class:1,percentage:1,_id:0}}]);
OUTPUT:

[
{ 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 }

7. Display name class and marks of first 3 toppers of TEComp A


db.student1.aggregate([{$match:{class:"TE Comp",div:"A"}},{$sort:{percentage:-
1}},{$limit:3},{$project:{name:1,class:1,percentage:1,_id:0}}]);
OUTPUT:
{ "name" : "Siya", "class" : "TE Comp", "percentage" : 82 }
{ "name" : "vidya", "class" : "TE Comp", "percentage" : 72 }
{ "name" : "diksha", "class" : "TE Comp", "percentage" : 55 }

8. Display the count of students in each division

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

Before creating index


library2018> db.student1.find({rollno : 1930},{}).explain();
{
explainVersion: '1',
queryPlanner: {
namespace: 'library2018.student1',
indexFilterSet: false,
parsedQuery: { rollno: { '$eq': 1930 } },
queryHash: '64BD6352',
planCacheKey: '64BD6352',
maxIndexedOrSolutionsReached: false,
maxIndexedAndSolutionsReached: false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'COLLSCAN',
filter: { rollno: { '$eq': 1930 } },
direction: 'forward'
},
rejectedPlans: []
},
command: {
find: 'student1',
filter: { rollno: 1930 },
projection: {},
'$db': 'library2018'
},
serverInfo: {
host: 'DESKTOP-4CV3UIP',
port: 27017,
version: '6.0.5',
gitVersion: 'c9a99c120371d4d4c52cbb15dac34a36ce8d3b1d'
},
serverParameters: {
internalQueryFacetBufferSizeBytes: 104857600,
internalQueryFacetMaxOutputDocSizeBytes: 104857600,
internalLookupStageIntermediateDocumentMaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryBytes: 104857600,
internalQueryMaxBlockingSortMemoryUsageBytes: 104857600,
internalQueryProhibitBlockingMergeOnMongoS: 0,
internalQueryMaxAddToSetBytes: 104857600,
internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600
},
ok: 1
}
After query execution

library2018> db.student1.createIndex({rollno : 1});


rollno_1
library2018> db.student1.find({rollno : 1930},{}).explain();
{
explainVersion: '1',
queryPlanner: {
namespace: 'library2018.student1',
indexFilterSet: false,
parsedQuery: { rollno: { '$eq': 1930 } },
queryHash: '64BD6352',
planCacheKey: '3EE1E79D',
maxIndexedOrSolutionsReached: false,
maxIndexedAndSolutionsReached: false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'FETCH',
inputStage: {
stage: 'IXSCAN',
keyPattern: { rollno: 1 },
indexName: 'rollno_1',
isMultiKey: false,
multiKeyPaths: { rollno: [] },
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: { rollno: [ '[1930, 1930]' ] }
}
},
rejectedPlans: []
},
command: {
find: 'student1',
filter: { rollno: 1930 },
projection: {},
'$db': 'library2018'
},
serverInfo: {
host: 'DESKTOP-4CV3UIP',
port: 27017,
version: '6.0.5',
gitVersion: 'c9a99c120371d4d4c52cbb15dac34a36ce8d3b1d'
},
serverParameters: {
internalQueryFacetBufferSizeBytes: 104857600,
internalQueryFacetMaxOutputDocSizeBytes: 104857600,
internalLookupStageIntermediateDocumentMaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryBytes: 104857600,
internalQueryMaxBlockingSortMemoryUsageBytes: 104857600,
internalQueryProhibitBlockingMergeOnMongoS: 0,
internalQueryMaxAddToSetBytes: 104857600,
internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600
},
ok: 1
}

Q3 Show existing indexes on the collection.

library2018> db.student1.getIndexes();
Output :

[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{ v: 2, key: { rollno: 1 }, name: 'rollno_1' }
]

Q4 Create customers collection in MongoDB containing documents in the following


format. Add atleast 10 documents and solve the given queries.
db.customers.insertOne({ name: "Suresh",
email: "suresh_k@gmail.com",
profession: "CA",
gender: "male",
FavColour: "Blue",
BillAmount:2550})

db.customers.insertOne({ name: "Samiksha",


email: "samikshak@gmail.com",
profession: "Computer
Engineer", gender: "female",
FavColour: "Black",
BillAmount:2552})

db.customers.insertOne({ name: "Sakshi",


email: "sakshi@gmail.com",
profession: "Computer
Engineer", gender: "female",
FavColour: "Pink",
BillAmount:1250})

db.customers.insertOne({ name: "Samir",


email: "samir@gmail.com",
profession: "Computer
Engineer", gender: "male",
FavColour: "Blue",
BillAmount:2550})

db.customers.insertOne({ name: "Sudir",


email: "sudir@gmail.com",
profession: "CA",
gender: "male",
FavColour: "Black",
BillAmount:1550})

db.customers.insertOne({ name: "Sanika",


email: "sanika@gmail.com",
profession: "CA",
gender: "female",
FavColour: "White",
BillAmount:2250})

db.customers.insertOne({ name: "Pratham",


email: "pratham@gmail.com",
profession: "Lawyer",
gender: "male",
FavColour: "Blue",
BillAmount: 1250})
db.customers.insertOne({ name: "Vaibhavi",
email: "vaibhavi@gmail.com",
profession: "Doctor",
gender: "female",
FavColour: "Blue",
BillAmount:550})

db.customers.insertOne({ name: "Swara",


email: "swara@gmail.com",
profession: "Doctor",
gender: "female",
FavColour: "Red",
BillAmount:1550})

db.customers.insertOne({ name: "Om",


email: "om@gmail.com",
profession: "CA",
gender: "male",
FavColour: "Black",
BillAmount:2220})

db.customers.insertOne({ name: "Kunal",


email: "kunal@gmail.com",
profession: "Electronic Engineer",
gender: "male",
FavColour: "Black",
BillAmount:1450})

db.customers.insertOne({ name: "Harshada",


email: "harshada@gmail.com",
profession: "CA",
gender: "female",
FavColour: "Purple",
BillAmount:1250})
db.customers.find();
1) Write a mapreduce query for finding the count of male and females in the customer collection
var mapper = function ()
... {
... emit(this.gender, 1);
... };

library2018> var reducer = function(key, emits)


... {
... return Array.sum(emits);
... };
library2018> db.customers.mapReduce(mapper,reducer, {out:"count_males_females" } ); {
result: 'count_males_females', ok: 1 }
library2018> db.count_males_females.find()
[ { _id: 'male', value: 6 }, { _id: 'female', value: 6 } ]

2) Write a mapreduce query for finding the count of each profession in the customer collection

library2018> var mapper = function () { var profession = this.profession.split(','); for (i in


profession) { emit(profession[i], 1); } };

library2018> var reducer = function (key, emits) {


... var count = 0;
... for (i in emits) {
... count += emits[i];
... }
... return count;
... };

library2018> db.customers.mapReduce(mapper,reducer,{ out:"results" });


{ result: 'results', ok: 1 }
library2018> db.results.find()
[
{ _id: 'CA', value: 5 },
{ _id: 'Lawyer', value: 1 },
{ _id: 'Computer Engineer', value: 3 },
{ _id: 'Doctor', value: 2 },
{ _id: 'Electronic Engineer', value: 1 }
]

3.Write a mapreduce query for finding the count of fields in each document in the customer
collection.

library2018> function map() { emit(this.name, Object.keys(this).length);


} [Function: map]
library2018> function reduce(key,values){return Array.sum(values)};

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> var mapper=function(){emit(this.name,this.BillAmount)}

library2018> var reducer=function(name,ArrayofEmits){return


Array.sum(ArrayofEmits)}

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 }
]

You might also like