Skip to content

Instantly share code, notes, and snippets.

@vseventer
Created June 6, 2012 14:26
Show Gist options
  • Select an option

  • Save vseventer/2882217 to your computer and use it in GitHub Desktop.

Select an option

Save vseventer/2882217 to your computer and use it in GitHub Desktop.
JavaScript Library aggregation leaderboard
// Scores collection
// [{ user: 'Alice', score: 2 },
// { user: 'Alice', score: 1 },
// { user: 'Bob', score: 4 }]
// Create leaderboard
var agg = new Kinvey.Aggregation();
agg.on('user');
agg.setInitial({ total: 0 });
agg.setReduce(function(doc, out) {
out.total += doc.score;
});
var collection = new Kinvey.Collection('scores');
collection.aggregate(agg, {
success: function(response) {
// response holds:
// [{ name: 'Alice', total: 3 },
// {name: 'Bob', total: 4 }]
},
error: function(error) {
// Oops. An error occured.
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment