Skip to content

Instantly share code, notes, and snippets.

@gautamrege gautamrege/map_rereduce.js
Last active Dec 15, 2015

Embed
What would you like to do?
Map ReReduce and join example
map = function() {
if (this.is_cancelled == false) {
data = { bookings: [ this._id ],
booking_amount: this.booking_amount,
amt_received: 0,
cancellations: 0
}
} else {
data = { bookings: [],
booking_amount: 0,
amt_received: 0,
cancellations: 1
}
emit(this.resource_id, data);
};
reduce = function(key, values) {
var r = { bookings: [], booking_amount: 0, amt_received: 0, cancellations: 0 }
values.forEach(function(value) {
r.booking_amount += value.booking_amount;
r.amt_received += value.amt_received;
r.cancellations += value.cancellations;
if(value.bookings.length > 0) {
r.bookings = r.bookings.concat(value.bookings);
}
});
return r;
};
db.bookings.mapReduce(map, reduce,
{
query: { resource_type: "Distributor" },
out: 'distributorReport'
}
);
mapStat = function() {
emit('booking_ids', { bookings: this.value.bookings });
}
reduceStat = function(key, values) {
r = { bookings: [] }
values.forEach(function(value) {
r.bookings = r.bookings.concat(value.bookings);
});
return r;
}
db.distributorReport.mapReduce(mapStat, reduceStat, { out: "tmpBookings" } );
mapVoucher = function() {
emit(this.resource_id, { bookings: [], booking_amount: 0, amt_received: this.credit_amount, cancellations: 0 });
}
db.vouchers.mapReduce(mapVoucher, reduce,
{
query: { resource_type: "Distributor", booking_id: { $in: db.tmpBookings.findOne().value.bookings } },
out: { reduce: 'distributorReport'}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.