On Thursday, January 3, 2013 11:40:52 PM UTC+11, Sam Halliday wrote:
Thanks Sam, I'll look into passing a sorted query to the MR, I didn't realise that was possible.
Hi Sam,
I'm not sure you need a Map/Reduce for this. Are there extra calculations being done?
Since you already have the score calculated for each user, I think you would just do a find() which returns results in the desired ranking order and iterate the results to update the ranking for each user.
Some example JavaScript code (mongo shell):
// Calculate rankings based on score (descending)
var userRank = 1;
db.users.find().sort({score: -1}).forEach(
function(user) {
db.users.update({_id:user._id}, {$set: { ranking: userRank }});
userRank++;
}
)
// Top 10 users
db.users.find().sort({ranking: 1}).limit(10);
// Bottom 10 users
db.users.find().sort({ranking: -1}).limit(10);
// Find users +/- from my ranking in the leaderboard
var myRanking = 10; var nearbyRanking = 5;
db.users.find({ $and: [
{ ranking: { $gte: myRanking - nearbyRanking }},
{ ranking: { $lte: myRanking + nearbyRanking }},
]}).sort({ranking:1})
Cheers,
Stephen