嗨,我有一个 dynamodb 表。我希望该服务返回该表中的所有项目,并且顺序是按一个属性排序。
我需要为此创建一个全局二级索引吗?如果是这样的话,哈希键应该是什么,范围键是什么? (请注意,对 gsi 的查询必须在 GSI 的哈希键上指定“EQ”比较器。)
非常感谢!
埃尔本
最佳答案
如果您知道 HashKey,则任何查询都将返回按 Range 键排序的项目。来自 documentation :
Query results are always sorted by the range key. If the data type of the range key is
Number
, the results are returned in numeric order. Otherwise, the results are returned in order of UTF-8 bytes. By default, the sort order is ascending. To reverse the order, set theScanIndexForward
parameter set tofalse
.
现在,如果您需要退回所有元素,您应该使用扫描。您无法订购扫描结果。
另一个选项是使用 GSI ( example )。在这里,您可以看到 GSI 仅包含 HashKey。我猜结果将按照该键的排序顺序(我还没有在程序中检查这部分!)。
关于amazon-web-services - Dynamodb 按排序顺序扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794945/