Boto3:在DynamoDB中使用“ NOT IN”进行扫描 [英] Boto3: use 'NOT IN' for Scan in DynamoDB
本文介绍了Boto3:在DynamoDB中使用“ NOT IN”进行扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设法制作了一个过滤器表达式,用于过滤扫描中的项目。像这样的东西:
I've managed to make a filter expression for filtering items from Scan. Smth like:
users = [1, 2, 3]
table.scan(
FilterExpression=Attr('user_id').is_in(users)
)
我能以某种方式将其转换吗从过滤到排除,所以我将获得所有ID为1、2、3的用户。
Can I somehow convert it from filtering to excluding, so I will get all users except those with ids 1, 2, 3.
推荐答案
我唯一的方法到目前为止发现的是使用boto3.client代替表和低级语法。像这样的东西:
The only way I have found so far is to use boto3.client instead of table and low-level syntax. Smth like this:
lst_elements = ''
attr_elements = {}
for id in user_ids:
lst_element += 'user' + str(id)
attr_elements['user' + str(id)] = id
client.scan(
TableName='some_table_name',
FilterExpression="NOT (user_id IN ({}))".format(lst_element[:-1]),
ExpressionAttributeValues=attr_elements
)
此解决方案对我来说不错,但看起来确实很复杂。因此,如果您知道一种更好的方法,请添加您的答案。
This solution works fine for me but looks really complicated. So if you know a better way to do it, please add your answer.
这篇关于Boto3:在DynamoDB中使用“ NOT IN”进行扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文