Reputation: 1260
I am currently trying to use the dynamodb extension in python to get the position\rank in a descending query on an index, the following is what I have come up with:
router.get('/'+USER_ROUTE+'/top', (req, res) => {
POST.query()
.usingIndex('likecount')
.attributes(['id', 'likecount'])
.descending()
.loadAll()
.exec((error, result) => {
if (error) {
res.status(400).json({ error: 'Error retrieving most liked post' });
}
//convert res to dict and sort dict by value (likecount)
res.json({position:Object.keys(result).indexOf(req.body.id)});
});
});
As you can see, I would convert the result into a dict of ID and likecount and then sort the dict after which I get the index of the key I am looking for. Obviously this fails in multiple respects, it is slow/inefficient (iterates over every item in the database per call) and requires multiple steps.
Is there a more succinct method to achieve this? Thanks.
Upvotes: 0
Views: 603
Reputation: 1343
DynamoDB is not design for leaderboard. You should choose another DB for this purpose. I suggest you use "DynamoDB" and "Redis" in your case. You can ref this document to know about Redis leaderboard: https://redislabs.com/redis-enterprise/use-cases/leaderboards/
Workflow:
Upvotes: 1
Reputation: 14003
There isn't a better way to do this. DynamoDB isn't an appropriate database to use in scenarios where you need complex querying capabilities.
Additionally, a single query operation has a strict limit on how many documents it can read; even if it did support this kind of querying, it wouldn't work for scenarios where you have more documents than it can process in a single read.
Upvotes: 1