Skip to content

Instantly share code, notes, and snippets.

@tschaub
Created January 16, 2013 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tschaub/4544682 to your computer and use it in GitHub Desktop.
Save tschaub/4544682 to your computer and use it in GitHub Desktop.
Mongo map-reduce experiments
function mapFunc() {
var i, ii, key, doc, service;
for (i = 0, ii = this.resources.length; i < ii; ++i) {
key = this.resources[i];
doc = {
services: {}
};
service = this.service || '';
doc.services[service] = 1;
emit(key, doc);
}
}
function reduceFunc(key, docs) {
var i, ii, o, doc, service, reduced = {services: {}};
for (i = 0, ii = docs.length; i < ii; ++i) {
doc = docs[i];
for (service in doc.services) {
if (!(service in reduced.services)) {
reduced.services[service] = 1;
} else {
reduced.services[service] += 1;
}
}
}
return reduced;
}
db.message.mapReduce(mapFunc, reduceFunc, {out: 'service_count_per_resource'});
// aggregate by day
// db.message.ensureIndex({"error": 1});
var start = (new Date).getTime();
db.message.aggregate({
$limit: 1000000
}, {
$match: {error: {$ne: null}}
}, {
$project: {
year: {$year: '$date'},
day: {$dayOfYear: '$date'}
}
}, {
$group: {
_id: {year: '$year', day: '$day'},
error_count: {$sum: 1}
}
});
var duration = (new Date).getTime() - start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment