-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asgn_11.js
49 lines (39 loc) · 1.85 KB
/
Asgn_11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
db.createCollection('Journal');
db.Journal.insert({'book_id':1,'book_name':'JavacdOOP','amt':500,'status':'Available'});
db.Journal.insert({'book_id':1,'book_name':'JavaOOP','amt':400,'status':'NotAvailable'});
db.Journal.insert({'book_id':1,'book_name':'Java','amt':300,'status':'NotAvailable'});
db.Journal.insert({'book_id':2,'book_name':'Java','amt':300,'status':'Available'});
db.Journal.insert({'book_id':2,'book_name':'OPP','amt':200,'status':'Available'});
db.Journal.insert({'book_id':2,'book_name':'C+','amt':200,'status':'Available'});
db.Journal.insert({'book_id':3,'book_name':'C+','amt':150,'status':'Available'});
db.Journal.insert({'book_id':3,'book_name':'C++','amt':200,'status':'NotAvailable'});
db.Journal.insert({'book_id':4,'book_name':'OPP C++','amt':300,'status':'Not Available'});
db.Journal.insert({'book_id':5,'book_name':'OPPC++','amt':400,'status':'Available'});
db.Journal.insert({'book_id':5,'book_name':'C++','amt':400,'status':'Available'});
db.Journal.insert({'book_id':5,'book_name':'C++Java','amt':400,'status':'NotAvailable'});
var mapfunction=function(){ emit(this.book_id,this.amt)};
var reducefunction=function(key,value){return Array.sum(value);};
db.Journal.mapReduce(mapfunction,reducefunction, {'out':'new'});
// {
// "result" : "new",
// "timeMillis" : 49,
// "counts" : {
// "input" : 12,
// "emit" : 12,
// "reduce" : 4,
// "output" : 5
// }
// "ok" : 1
// }
db.Journal.mapReduce(mapfunction,reducefunction,{'out':'new'}).find().pretty();
// { "_id" : 1, "value" : 1200}
// { "_id" : 2, "value" : 700 }
// { "_id" : 3, "value" : 350 }
// { "_id" : 4, "value" : 300 }
// { "_id" : 5, "value" : 1200}
db.new.find().pretty();
// { "_id" : 1, "value" : 1200 }
// { "_id" : 2, "value" : 700 }
// { "_id" : 3, "value" : 350 }
// { "_id" : 4, "value" : 300 }
// { "_id" : 5, "value" : 1200 }