Skip to content

Instantly share code, notes, and snippets.

@jiangzhuo
Created December 20, 2012 12:10
Show Gist options
  • Save jiangzhuo/4344952 to your computer and use it in GitHub Desktop.
Save jiangzhuo/4344952 to your computer and use it in GitHub Desktop.
function Map() {
emit(
{ClientVersion:this.ClientVersion, PublisherId:this.PublisherId}, // how to group
{SessionCount: 1, userIds:[this.UserId]} // associated data point (document)
);
}
function Reduce(key, values) {
var reduced = {SessionCount:0,userIds:{}}; // initialize a doc (same format as emitted value)
values.forEach(function(val) {
reduced.SessionCount += val.SessionCount; // reduce logic
val.userIds.forEach(function(uid){
if(!reduced.userIds[uid]){
reduced.userIds[uid]=true;
}
});
});
return reduced;
}
function Finalize(key, reduced) {
return {UserCount:reduced.userIds.length, SessionCount:reduced.SessionCount};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment