英文:
Lambda DynamoDB "x and (y or z)" Query
问题
KeyConditionExpression: '要查询的条件:#list = :listID and (#owner = :userID or #collab = :userID)';
list
已创建索引。
如果我只有一个额外的参数可用,我可以将其添加为索引的排序键,但是如果我需要两个呢?
我需要为 owner
和 collab
都添加索引吗?那么在查询时我应该将什么作为 IndexName
?
英文:
I need to create a dynamoDB query that fetches results based on an index, but only if 1 of 2 conditions are met.
Something like this:
KeyConditionExpression: '#list = :listID and (#owner = :userID or #collab = :userID)'
list
is indexed.
I understand if I had only 1 extra param to use, I could add that as the sort key on the index, but what when I need 2?
Do I need to add indexes for both owner
and collab
, but then what do I put as the IndexName
for the query?
答案1
得分: 1
你不能高效地在键上执行 OR
表达式。你应该以这样一种方式建模你的数据,使分区键和排序键模型化大多数被评估的数据,然后你可以使用 FilterExpression 来细化剩余的数据。
或者简单地这样应用它:
KeyConditionExpression: '#list = :listID'
FilterExpression: '#owner = :userID or #collab = :userID'
英文:
You cannot do an OR
expression on keys efficiently. You should model your data in such a way that the partition key and sort key model the majority of the evaluated data, then you can use a FilterExpression to refine the remainder of the data.
Or simply apply it like this:
KeyConditionExpression: '#list = :listID'
FilterExpression: '#owner = :userID or #collab = :userID)'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论