使用pynamodb,我希望获得与表中某个散列键匹配的所有范围键。
我知道我可以进行一次扫描,然后过滤出与散列键匹配的条目,如下所示:
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute
class Users(Model):
class Meta:
table_name = 'user_posts'
username = UnicodeAttribute(hash_key=True)
post_id = UnicodeAttribute(range_key=True)
# Get all post_id's for a username
user = 'johndoe22'
posts = []
for entry in Users.scan():
if entry.username == user:
posts.append(entry.post_id)我希望将过滤逻辑移动到查询级别,这样我就不必拉下db的全部内容。我如何才能做到这一点?
发布于 2017-10-06 22:03:25
在这种情况下,您需要使用查询而不是扫描操作。我对python不太熟悉,所以我不能为您提供帮助,但是当您想要使用范围/散列键进行查询时,扫描操作很可能不是您想要使用的。请看一下查询操作:http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
https://stackoverflow.com/questions/46505997
复制相似问题