DynamoDB GSI用于布尔值

发布于 2025-02-05 07:43:08 字数 845 浏览 3 评论 0原文

因此,我有此通知带有以下列的表:

  • PK :(存储userId)
  • 该存储日期已发送已发送的日期)
  • 数据:(它存储通知的数据)
  • Sentat :( :(一个布尔值告诉用户是否已读取特定的通知)

我想创建一个GSI,以获取未读取的特定用户的所有通知(read:read:false) 因此,分区密钥将是userId,排序键将是读取,但这里的问题是,我无法为sort键提供布尔值,以便能够查询尚未阅读通知的用户。 这可以进行扫描,但这不是我要实现的结果。谁能帮我吗?谢谢

const params ={
      TableName: await this.configService.get('NOTIFICATION_TABLE'),
      FilterExpression: '#PK = :PK AND #Read = :Read',
      ExpressionAttributeNames: {
        '#PK': 'PK',
        '#Read': 'Read',
      },
      ExpressionAttributeValues: {
        ':PK': 'NOTIFICATION#a8a8e4c7-cab0-431e-8e08-1bcf962358b8',
        ':Read': true, *//this is causing the error*
      },
    };
    const response = await this.dynamoDB.scan(params).promise();
    

So I have this notifications table with the following columns:

  • PK: (which stores the userId)
  • sentAt: (which stores the date the notifications was sent)
  • data: (which stores the data of the notification)
  • Read: (a boolean value which tells if the user has read the specific notification)

I wanted to create a GSI to get all the notification from a specific user that are not read (Read: False)
So the partition key would be userId and the sort key would be Read but the issue here is that I cannot give a boolean value to the sort key to be able to query the users that have not read the notifications.
This works with scan but that is not the result I am trying to achieve. Can anyone help me on this? Thanks

const params ={
      TableName: await this.configService.get('NOTIFICATION_TABLE'),
      FilterExpression: '#PK = :PK AND #Read = :Read',
      ExpressionAttributeNames: {
        '#PK': 'PK',
        '#Read': 'Read',
      },
      ExpressionAttributeValues: {
        ':PK': 'NOTIFICATION#a8a8e4c7-cab0-431e-8e08-1bcf962358b8',
        ':Read': true, *//this is causing the error*
      },
    };
    const response = await this.dynamoDB.scan(params).promise();
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2025-02-12 07:43:08

是的,我们不能拥有bool类型值以用作DynamoDB分区密钥或分类密钥。

您实际上可以考虑的一些替代方案:

  1. 仅使用分区密钥创建GSI,gsi-userid。进行查询时,您可以使用userId查询,然后通过读取过滤。这至少将帮助您节省一些成本,因为您不需要扫描整个桌子。但是,请注意热门分区。 link

  2. 数据类型字符串而不是。例如,它可能是yn之类的值。因此,您将能够使用gsi-userid-read创建GSI,这将满足您的需求。

Yes, we cannot have bool type value to be used as DynamoDB Partition Key or Sort Key.

Some alternatives you could actually consider:

  1. Create a GSI with only Partition Key, gsi-userId. When you do the query, you can query with userId and filter by Read. This will at least help you in saving some costs as you do not need to scan the whole table. However, be aware of Hot Partitions. Link

  2. Consider changing the Read data type to string instead. E.g. It could be values such as Y or N only. As such, you will be able to create a GSI with gsi-userId-Read and this would fulfill what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文