-
Notifications
You must be signed in to change notification settings - Fork 1
/
JoinCollections.js
107 lines (90 loc) · 2.41 KB
/
JoinCollections.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var mongoose = require('mongoose');
require('./AcmeSchemas.js')();
mongoose.connect('mongodb://localhost/acme');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(){
console.log('Connected to acme!')
});
var Transaction = mongoose.model('Transaction');
var Product = mongoose.model('Product');
var tOptions = {};
var pOptions = {};
tOptions.map = function() {
var value = {
color : '',
product_id : '',
transactions : {
too_small : 0,
too_large : 0,
dont_like : 0,
order : 0
}
};
if(this.transaction_type === 'Return'){
if(this.return_reason === 'too_small'){
value.transactions.too_small = this.quantity;
}else if(this.return_reason === 'too_large'){
value.transactions.too_large = this.quantity;
}else if(this.return_reason === 'dont_like'){
value.transactions.dont_like = this.quantity;
}
}else if(this.transaction_type === 'Order'){
value.transactions.order = this.quantity;
}
emit(this.variant_id, value);
};
pOptions.map = function() {
var value = {
color : this.color,
product_id : this.product_id,
transactions : {
too_small : 0,
too_large : 0,
dont_like : 0,
order : 0
}
}
emit(this.variant_id, value);
};
var reduce = function(key, values){
var result = {
color : '',
product_id : '',
transactions : {
too_small : 0,
too_large : 0,
dont_like : 0,
order : 0
}
};
values.forEach(function(value){
if (value.color !== ''){
result.color = value.color;
}
if (value.product_id !== ''){
result.product_id = value.product_id;
}
result.transactions.too_small += value.transactions.too_small;
result.transactions.too_large += value.transactions.too_large;
result.transactions.dont_like += value.transactions.dont_like;
result.transactions.order += value.transactions.order;
});
return result;
}
tOptions.reduce = reduce;
pOptions.reduce = reduce;
tOptions.out = {
reduce: 'joinedCollection'
}
pOptions.out = {
reduce: 'joinedCollection'
}
tOptions.verbose = true;
pOptions.verbose = true;
Product.mapReduce(pOptions, function (err, results, stats) {
console.log('map reduce took %d ms', stats.processtime)
});
Transaction.mapReduce(tOptions, function (err, results, stats) {
console.log('map reduce took %d ms', stats.processtime)
});