DynamoDB查询带有筛选条件不返回任何项。

huangapple go评论148阅读模式
英文:

dynamodb query with filters returns no items

问题

id(string) message(string) created(number) completed(boolean)
123 指示 1686731293161 -
... ... ... ...
129 指示 1686821275413 false

分区键: 消息<br />
排序键: 创建时间<br />
投影键: 所有<br />

对于表格的总结:

  • 有些条目没有消息和已完成属性
  • 即使消息在 GSI 中用作分区键,对于我的用例来说没问题

我尝试使用 aws-sdk dynamodb 客户端查询,参数如下:

  1. TableName: &quot;test-idStatus&quot;,
  2. IndexName: &quot;messageCreatedIndex&quot;,
  3. KeyConditionExpression: &quot;#message = :message and #created BETWEEN :createdfrom AND :createdto&quot;,
  4. ExpressionAttributeNames: {
  5. &quot;#message&quot;: &quot;message&quot;,
  6. &quot;#created&quot;: &quot;created&quot;,
  7. &quot;#completed&quot;: &quot;completed&quot;,
  8. },
  9. ExpressionAttributeValues: {
  10. &quot;:messageFunction&quot;: &quot;Instruction&quot;,
  11. &quot;:createdfrom&quot;: 1686817506000, // 2023 年 6 月 15 日 08:25
  12. &quot;:createdto&quot;: 1686821106000, // 2023 年 6 月 15 日 09:25
  13. &quot;:completed&quot;: &quot;false&quot;,
  14. },
  15. ScanIndexForward: false,
  16. Limit: 5,
  17. Select: &quot;ALL_ATTRIBUTES&quot;,
  18. FilterExpression: &quot;#completed = :completed&quot;,
  19. }
  20. ``` 这返回了条目。
  21. 但是当我将 `ExpressionAttributeValues` 中的以下值更改为:&lt;br/&gt;
  22. `createdfrom`: 1686731286000 (2023 年 6 月 14 日 08:25) &lt;br/&gt;
  23. 这没有返回任何条目(但返回了 LastEvaluatedKey)。&lt;br/&gt;
  24. 这很奇怪,因为 6 月 15 日返回了条目,只是 6 月 14 日到 6 月 15 日的条目的子集。
  25. 我还尝试在 AWS 控制台 DynamoDB 中复制这个问题,结果相同。
  26. 然后我还尝试移除 `ScanIndexForward`,现在返回了条目。
  27. <details>
  28. <summary>英文:</summary>
  29. |id(string)|message(string)|created(number)|completed(boolean)|
  30. |-|-|-|-|
  31. |123|Instruction|1686731293161|-|
  32. |...|...| ...|...|
  33. |129|Instruction|1686821275413|false|
  34. `Partition key`: message&lt;br /&gt;
  35. `Sort key`: created&lt;br /&gt;
  36. `Projected keys`: All&lt;br /&gt;
  37. In summary for the table:
  38. - some entries does not have message and completed attributes
  39. - even though message is used as partition key in GSI, this is ok for my use case
  40. I tried to query using aws-sdk dynamodb client with this params:
  41. ```{
  42. TableName: &quot;test-idStatus&quot;,
  43. IndexName: &quot;messageCreatedIndex&quot;,
  44. KeyConditionExpression: &quot;#message = :message and #created BETWEEN :createdfrom AND :createdto&quot;,
  45. ExpressionAttributeNames: {
  46. &quot;#message&quot;: &quot;message&quot;,
  47. &quot;#created&quot;: &quot;created&quot;,
  48. &quot;#completed&quot;: &quot;completed&quot;,
  49. },
  50. ExpressionAttributeValues: {
  51. &quot;:messageFunction&quot;: &quot;Instruction&quot;,
  52. &quot;:createdfrom&quot;: 1686817506000, // 2023-June-15 08:25
  53. &quot;:createdto&quot;: 1686821106000, // 2023-June-15 09:25
  54. &quot;:completed&quot;: &quot;false&quot;,
  55. },
  56. ScanIndexForward: false,
  57. Limit: 5,
  58. Select: &quot;ALL_ATTRIBUTES&quot;,
  59. FilterExpression: &quot;#completed = :completed&quot;,
  60. }

This returned entries

But when I changed the ExpressionAttributeValues of:<br/>
createdfrom: 1686731286000 (2023-June-14 08:25) <br/>
This did not return any entries (but returns a LastEvaluatedKey).<br/>
Which is weird since June-15 returned entries which is just a subset of June-14 to June-15 entries

I also tried to replicate this in the AWS console dynamoDB,
The same thing happened.

Then I also tried removing ScanIndexForward, now it returned entries.

答案1

得分: 2

发生的情况是你设置了一个FilterExpression和一个Limit。现在发生的是DynamoDB读取了5个项目,因为这是你的Limit,然后将你的过滤器应用于这5个项目,但没有一个匹配表达式,所以你得到了一个空响应。

更改日期只是简单地改变了读取的5个项目,很幸运它们匹配了你的过滤器。更改扫描方向也是一样的,你只是改变了前5个项目。

英文:

What's happening here is that you set a FilterExpression and a Limit. What happens now is that DynamoDB reads 5 items as that is your Limit and then applies your filter to those 5 items but none match the expression so you get an empty response.

Changing the date is simply changing the 5 items read and it's just luck that they match your filter. The same goes for changing the scan direction, you're just changing the first 5 items.

huangapple
  • 本文由 发表于 2023年6月16日 11:33:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定