Implementing a leader-board using MongoDB

698 views
Skip to first unread message

venu

unread,
Jul 8, 2010, 6:14:20 PM7/8/10
to mongodb-user
Hi,
I was thinking about implementing a leaderboard that, at any given
time, contains only the top 20 richest people on the planet. So, for
example:

richest = { "countries" : { "USA" : [ ["Bill Gates", 5000000000000],
["Warren Buffet, 4999999999999] ]... }, "UK" : { ....} ... }

now when one of these rich people's stock holding or asset's value
goes up or down it should change their position in the leaderboard
too.

So, addAssetValueDelta("MongoDBOwner",300000000000000000000) should
leapfrog "MongoDBOwner" onto the top spot in USA.

1) Should I just findAndModify on MongoDBOwner with upsert=true, sort
the doc-or array in mongodb and chop entry greater than twenty-first
entry or is it better to implement a circular list algorithm in python
and then save the document (or array) back to mongodb

Thanks,
Venu.

roger

unread,
Jul 9, 2010, 12:18:41 AM7/9/10
to mongodb-user
Hi Venu,

How about creating a people collection. Each person can have a wealth
field that you can update as their wealth goes up and down.

p= { name:"gates", wealth:500}
db.people.save(p)

db.people.update({name:"gates"}, {$set : {wealth:350}})

Getting the 20 richest people is now as simple as

db.people.find().sort({wealth:-1}).limit(20)


-roger

venu

unread,
Jul 9, 2010, 11:06:43 AM7/9/10
to mongodb-user
Hi Roger,
Thanks, that should work.
Venu.
Reply all
Reply to author
Forward
0 new messages