Created
July 21, 2015 12:38
-
-
Save yetithefoot/c529741623e577bb810d to your computer and use it in GitHub Desktop.
faster mr for picsio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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