Skip to content

Instantly share code, notes, and snippets.

@yetithefoot
Created July 21, 2015 12:38
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 yetithefoot/c529741623e577bb810d to your computer and use it in GitHub Desktop.
Save yetithefoot/c529741623e577bb810d to your computer and use it in GitHub Desktop.
faster mr for picsio
var query = { user: ObjectId("54ef43552e90860000000001"), trashed: { $ne: true } };
function map(){
if(this.tags && this.tags.length) {
var emits = []; // store all future emits. we will unique it later
for(var i=0; i<this.tags.length;i++){
var tag = this.tags[i];
var path = tag.path;
while(path.length){
emits[path] = 1; // no need to count similar tags twice
var lastDelimiter = path.lastIndexOf('/');
path = path.slice(0, lastDelimiter);
}
}
// emit accumulate keys
for(var k in emits) emit(k, {count:1});
} else {
emit('/root', {count:1});
}
}
var reduce = function(key, values) {
var sum = 0;
values.forEach(function(value) {
sum += value['count'];
});
return {count: sum};
}
db.images.mapReduce(map, reduce, {out: {inline:1}, query:query})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment