0% found this document useful (0 votes)
5 views5 pages

Map Reduce

The document contains a PowerShell session where the user connects to a MongoDB database and performs various operations, including querying student data and executing map-reduce functions to calculate total and average marks. It also includes warnings about access control and deprecation of the mapReduce method. The results show the total and average marks for students categorized into different classes based on their total marks.

Uploaded by

hemantrathod0112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Map Reduce

The document contains a PowerShell session where the user connects to a MongoDB database and performs various operations, including querying student data and executing map-reduce functions to calculate total and average marks. It also includes warnings about access control and deprecation of the mapReduce method. The results show the total and average marks for students categorized into different classes based on their total marks.

Uploaded by

hemantrathod0112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Windows PowerShell

Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Users\HP> mongosh

Current Mongosh Log ID: 653a0e1d5f62b60a34f4a231

Connecting to: mongodb://127.0.0.1:27017/?


directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.2

Using MongoDB: 7.0.0

Using Mongosh: 2.0.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------

The server generated these startup warnings when booting

2023-10-16T01:34:24.222+05:30: Access control is not enabled for the database. Read and write
access to data and configuration is unrestricted

------

test> use Shubham

switched to db Shubham

Shubham> db.stud.find({})

_id: ObjectId("65394c4179bc104fb33a3d59"),

rno: 16,

name: 'Shubham',

subject: 'DBMS',

marks: 45

},

{
_id: ObjectId("65394c4179bc104fb33a3d5a"),

rno: 16,

name: 'Shubham',

subject: 'CNS',

marks: 40

},

_id: ObjectId("65394c4179bc104fb33a3d5b"),

rno: 16,

name: 'Shubham',

subject: 'TOC',

marks: 30

},

_id: ObjectId("65394c9c79bc104fb33a3d5c"),

rno: 13,

name: 'Ram',

subject: 'CNS',

marks: 45

},

_id: ObjectId("65394c9c79bc104fb33a3d5d"),

rno: 16,

name: 'Ram',

subject: 'TOC',

marks: 40

},

_id: ObjectId("65394c9c79bc104fb33a3d5e"),

rno: 13,

name: 'Ram',
subject: 'DSA',

marks: 45

},

_id: ObjectId("65394cd979bc104fb33a3d5f"),

rno: 16,

name: 'Pratik',

subject: 'DBMS',

marks: 40

},

_id: ObjectId("65394cd979bc104fb33a3d60"),

rno: 11,

name: 'Pratik',

subject: 'CNS',

marks: 50

},

_id: ObjectId("65394cd979bc104fb33a3d61"),

rno: 11,

name: 'Pratik',

subject: 'TOC',

marks: 20

},

_id: ObjectId("65394d1979bc104fb33a3d62"),

rno: 1,

name: 'Sid',

subject: 'DBMS',

marks: 30

},
{

_id: ObjectId("65394d1979bc104fb33a3d63"),

rno: 11,

name: 'Sid',

subject: 'CNS',

marks: 30

},

_id: ObjectId("65394d1979bc104fb33a3d64"),

rno: 1,

name: 'Sid',

subject: 'TOC',

marks: 50

Shubham> var mapFunction = function() { emit( this.name , this.marks);};

Shubham> var reduceFunction = function( name , marks ){ return Array.sum(marks);};

Shubham> db.stud.mapReduce(mapfunction , reduceFunction , {out:"totalMarks"})

ReferenceError: mapfunction is not defined

Shubham> db.stud.mapReduce(mapFunction , reduceFunction , {out:"totalMarks"})

DeprecationWarning: Collection.mapReduce() is deprecated. Use an aggregation instead.

See https://docs.mongodb.com/manual/core/map-reduce for details.

{ result: 'totalMarks', ok: 1 }

Shubham> db.totalMarks.find({})

{ _id: 'Shubham', value: 115 },

{ _id: 'Pratik', value: 110 },

{ _id: 'Sid', value: 110 },

{ _id: 'Ram', value: 130 }


]

Shubham> var reduceAvg = function( name , marks ){ return Array.avg(marks);};

Shubham> db.stud.mapReduce(mapFunction , reduceAvg , {out:"avgMarks"})

{ result: 'avgMarks', ok: 1 }

Shubham> db.avgMarks.find({}).sort({value:-1})

{ _id: 'Ram', value: 43.333333333333336 },

{ _id: 'Shubham', value: 38.333333333333336 },

{ _id: 'Pratik', value: 36.666666666666664 },

{ _id: 'Sid', value: 36.666666666666664 }

Shubham>

var reduceCustom = function (name , marks ) {

var totalMarks = Array.sum(marks);

if(totalMarks <115 ){

return "Class C"; }

else if (totalMarks >= 115 && totalMarks <=120){

return "Class B";}

else if(totalMarks >120){

return "Class A";}

else{

return "Fail";}

db.stud.mapReduce(mapFunction , reduceCustom , { out: "Class"})

You might also like