【发布时间】:2020-01-04 14:28:54
【问题描述】:
我目前正在尝试在 python 中使用dynamodb 扩展来获取索引降序查询中的位置\排名,以下是我想出的:
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)});
});
});
如您所见,我会将结果转换为 ID 和 likecount 的 dict,然后对 dict 进行排序,然后得到我要查找的键的索引。 显然,这在多个方面都失败了,它速度慢/效率低(每次调用都会遍历数据库中的每个项目)并且需要多个步骤。
有没有更简洁的方法来实现这一点? 谢谢。
【问题讨论】:
标签: python nosql amazon-dynamodb