DynamoDB GSI用于布尔值
因此,我有此通知
带有以下列的表:
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我们不能拥有
bool
类型值以用作DynamoDB分区密钥或分类密钥。您实际上可以考虑的一些替代方案:
仅使用分区密钥创建GSI,
gsi-userid
。进行查询时,您可以使用userId
查询,然后通过读取
过滤。这至少将帮助您节省一些成本,因为您不需要扫描整个桌子。但是,请注意热门分区。 link数据类型
字符串
而不是。例如,它可能是y
或n
之类的值。因此,您将能够使用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:
Create a GSI with only Partition Key,
gsi-userId
. When you do the query, you can query withuserId
and filter byRead
. 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. LinkConsider changing the
Read
data type tostring
instead. E.g. It could be values such asY
orN
only. As such, you will be able to create a GSI withgsi-userId-Read
and this would fulfill what you need.