DynamoDB排序键未按排序顺序返回数据。

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

Dynamodb sort key not returning data in sorted order

问题

我已创建了一个排序键 SK,我正在使用 begins_with 查询来检索消息。

SK 被添加为 "message_<UTC 日期_时间>_chatid"
在这里,日期 1 月 5 日出现在 1 月 4 日之前,但为了按日期倒序检索数据,它应该在 1 月 4 日之后。
要按日期倒序检索消息,您可以做以下操作:

params = {
  TableName: "test_chat",
  ScanIndexForward: false,
  KeyConditionExpression: "PK = :pkval and begins_with(SK, :skval)",
  ExpressionAttributeValues: {
    ":pkval": chatId,
    ":skval": "message_",
  },
  Limit: 10,
};
let result = await dynamoDbClient.query(params).promise();
console.log(result);

这将按日期倒序检索消息。

英文:

I have created a sort key SK and I am retrieving messages using begins_with query on messsages

the SK is being added as "message_< UTC Date_TIME>_chatid"
DynamoDB排序键未按排序顺序返回数据。

here date 5 Jan is coming before 4 Jan but to retrieve data in sorted order it should be after 4 Jan
What can I do to retrieve the messages in sorted order according to the date in reverse order ,

 params = {
  TableName: &quot;test_chat&quot;,
  ScanIndexForward: false,

  KeyConditionExpression: &quot;PK = :pkval and begins_with(SK, :skval)&quot;,
  ExpressionAttributeValues: {
    &quot;:pkval&quot;: chatId,
    &quot;:skval&quot;: &quot;message_&quot;,
  },
  Limit: 10,
};
let result = await dynamoDbClient.query(params).promise();
console.log(result);

答案1

得分: 2

你的排序键是String类型。DynamoDB正在按照指示对字符串进行排序:message_Wed > message_Thurs

ISO 8601格式的日期字符串,如2023-01-05T12:22:33.034Z,更适合日期排序。

英文:

Your Sort Key is a String type. DynamoDB is sorting the strings as instructed: message_Wed > message_Thurs.

ISO 8601 format date strings like 2023-01-05T12:22:33.034Z are better suited to date sorting.

答案2

得分: 2

文档中:

> DynamoDB 使用基础的 UTF-8 字符串编码的字节进行字符串的排序和比较。例如,"a" (0x61) 大于 "A" (0x41),"¿" (0xC2BF) 大于 "z" (0x7A)。

> 您可以使用字符串数据类型来表示日期或时间戳。一种方法是使用 ISO 8601 字符串,如下例所示:

> - 2016-02-15
> - 2015-12-21T17:42:34Z
> - 20150311T122706Z

更多信息,请参阅wikipedia


您还可以将其称为字典排序。词汇排序通常指数学规则或排序方式,包括例如逻辑上证明排序是否可行。请在wikipedia上了解更多关于词汇顺序的信息。

字母顺序排序包括在处理空格、大写字符、数字和标点符号方面有所不同的变体。纯粹主义者认为允许使用除a-z以外的字符将使排序不再是"字母的",因此必须归为更大的"词汇的"类别。再次查看wikipedia以获取更多详细信息。

在计算机编程中,一个相关的问题是字典顺序ASCII 码顺序。在字典顺序中,大写字母"A"与小写字母"a"相邻排序。但是,在许多计算机语言中,默认的字符串比较将使用 ASCII 码。在 ASCII 中,所有大写字母都在所有小写字母之前,这意味着"Z"将在"a"之前排序。这有时被称为ASCIIbetical order

在您的示例中,它仅比较了ThuWed,因为它们是字符串的第一个唯一字符:

str1 = "message_Thu, 05 Jan 2023 11:27:16 GMT_3b60";
str2 = "message_Wed, 04 Jan 2023 10:43:16 GMT_3b60";

console.log(str1 > str2);

false
英文:

From the docs:

> DynamoDB collates and compares strings using the bytes of the
> underlying UTF-8 string encoding. For example, "a" (0x61) is greater
> than "A" (0x41), and "¿" (0xC2BF) is greater than "z" (0x7A).
>
> You can use the string data type to represent a date or a timestamp.
> One way to do this is by using ISO 8601 strings, as shown in these
> examples:
>
> - 2016-02-15
> - 2015-12-21T17:42:34Z
> - 20150311T122706Z

For more information, see [wikipedia](http://en.wikipedia.org/wiki/ISO_8601)


You can also call this Lexicographical sorting. The term lexicographical often refers to the mathematical rules or sorting. These include, for example, proving logically that sorting is possible. Read more about lexicographical order on wikipedia

Alphabetical ordering includes variants that differ in how to handle spaces, uppercase characters, numerals, and punctuation. Purists believe that allowing characters other than a-z makes the sort not "alphabetic" and therefore it must fall in to the larger class of "lexicographic". Again, wikipedia has additional details.

In computer programming, a related question is dictionary order or ascii code order. In dictionary order, the uppercase "A" sorts adjacent to lowercase "a". However, in many computer languages, the default string compare will use ascii codes. With ascii, all uppercase letters come before any lowercase letters, which means that that "Z" will sort before "a". This is sometimes called ASCIIbetical order.

In your example it is only comparing Thu and Wed as they are the first unique characters of your String:

str1 = &quot;message_Thu, 05 Jan 2023 11:27:16 GMT_3b60&quot;;
str2 = &quot;message_Wed, 04 Jan 2023 10:43:16 GMT_3b60&quot;;

console.log(str1&gt;str2);

false

答案3

得分: 0

使用KSUID作为标识
https://www.npmjs.com/package/ksuid 根据时间戳自动排序。

英文:

For Id, use KSUID
https://www.npmjs.com/package/ksuid Automatically sort based on timestamp.

huangapple
  • 本文由 发表于 2023年1月5日 19:37:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75017825.html
匿名

发表评论

匿名网友

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

确定