Skip to content

Instantly share code, notes, and snippets.

@wmelton
Forked from gatesvp/pivot.js
Created January 24, 2013 23:23
Show Gist options
  • Save wmelton/4629463 to your computer and use it in GitHub Desktop.
Save wmelton/4629463 to your computer and use it in GitHub Desktop.
db.foo.drop();
db.foo.insert( { actor: "Richard Gere", movies: ['Pretty Woman', 'Runaway Bride', 'Chicago'] })
db.foo.insert( { actor: "Julia Roberts", movies: ['Pretty Woman', 'Runaway Bride', 'Erin Brockovich'] })
map = function() {
for(var i in this.movies){
key = { movie: this.movies[i] };
value = { actors: [ this.actor ] };
emit(key, value);
}
}
reduce = function(key, values) {
actor_list = { actors: [] };
for(var i in values) {
actor_list.actors = values[i].actors.concat(actor_list.actors);
}
return actor_list;
}
printjson(db.foo.mapReduce(map, reduce, "pivot"));
db.pivot.find().forEach(printjson);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment