如何查询 DynamoDB table 以仅检索在字符串集属性中具有特定值的项目?
How to query a DynamoDB table to retreive only items which have a specific value inside an string-set attribute?
我的 table 中的项目具有 字符串集 类型的属性。我将坚持使用文档中的示例并将集合称为“颜色”。顾名思义,该集合包含代表每个项目颜色的各种字符串。这看起来像
this.
现在我想查询 table 以便检索集合中特定颜色的所有项目。因此,关于所附图片,我想查询颜色“绿色”,并希望收到商品 Picture2 和 Picture3。
有办法吗?
由于所有可能的颜色和项目的数量都很大,而且只有极少量的颜色与项目相关联,scan
的效率非常低。到目前为止,我尝试创建一个全局二级索引 (GSI),但它似乎无法按照我想要的方式创建,或者我错了吗?
除非您要搜索的字段已内置到主键或二级索引中,否则 scan
将是您唯一的选择。
scan
操作将允许您使用contains
关键字搜索集合
let params = {
TableName : 'TABLE_NAME',
FilterExpression: "contains(#color, :color)",
ExpressionAttributeNames: {
"#color": "color",
},
ExpressionAttributeValues: {
":color": "Blue",
}
};
documentClient.scan(params, function(err, data) {
console.log(data);
});
根据docs on secondary indexes,不能使用集合作为主键建立索引
The key schema for the index. Every attribute in the index key schema must be a top-level attribute of type String, Number, or Binary. Other data types, including documents and sets, are not allowed.
我的 table 中的项目具有 字符串集 类型的属性。我将坚持使用文档中的示例并将集合称为“颜色”。顾名思义,该集合包含代表每个项目颜色的各种字符串。这看起来像 this.
现在我想查询 table 以便检索集合中特定颜色的所有项目。因此,关于所附图片,我想查询颜色“绿色”,并希望收到商品 Picture2 和 Picture3。
有办法吗?
由于所有可能的颜色和项目的数量都很大,而且只有极少量的颜色与项目相关联,scan
的效率非常低。到目前为止,我尝试创建一个全局二级索引 (GSI),但它似乎无法按照我想要的方式创建,或者我错了吗?
除非您要搜索的字段已内置到主键或二级索引中,否则 scan
将是您唯一的选择。
scan
操作将允许您使用contains
关键字搜索集合
let params = {
TableName : 'TABLE_NAME',
FilterExpression: "contains(#color, :color)",
ExpressionAttributeNames: {
"#color": "color",
},
ExpressionAttributeValues: {
":color": "Blue",
}
};
documentClient.scan(params, function(err, data) {
console.log(data);
});
根据docs on secondary indexes,不能使用集合作为主键建立索引
The key schema for the index. Every attribute in the index key schema must be a top-level attribute of type String, Number, or Binary. Other data types, including documents and sets, are not allowed.