alp*_*ric 8 javascript python amazon-web-services amazon-dynamodb typescript
数据库中共有三项:
[
{
"year": 2013,
"info": {
"genres": ["Action", "Biography"]
}
},
{
"year": 2013,
"info": {
"genres": ["Crime", "Drama", "Thriller"]
}
},
{
"year": 2013,
"info": {
"genres": ["Action", "Adventure", "Sci-Fi", "Thriller"]
}
}
]
Run Code Online (Sandbox Code Playgroud)
由于该year属性是表的主键,我可以继续使用 来FilterExpression匹配确切的list值["Action", "Biography"]:
var params = {
TableName : TABLE_NAME,
KeyConditionExpression: "#yr = :yyyy",
FilterExpression: "info.genres = :genres",
ExpressionAttributeNames:{
"#yr": "year"
},
ExpressionAttributeValues: {
":yyyy": 2013,
":genres": ["Action", "Biography"]
}
};
Run Code Online (Sandbox Code Playgroud)
var AWS = require("aws-sdk");
var docClient = new AWS.DynamoDB.DocumentClient();
let promise = docClient.query(params).promise();
promise.then(res => {
console.log("res:", res);
})
Run Code Online (Sandbox Code Playgroud)
["Action", "Biography"]我宁愿进行查询以仅返回那些在存储在项目字段中的列表中包含字符串“Biography”的表项目,而不是匹配整个列表info.genres。我想知道这是否可以使用 DynamoDB queryAPI 实现?
工作解决方案(感谢 Balu)是使用QueryFilter contains比较运算符:
var params = {
TableName: TABLE_NAME,
Limit: 20,
KeyConditionExpression: "id = :yyyy",
FilterExpression: `contains(info.genres , :qqqq)`,
ExpressionAttributeValues: {
":qqqq": { S: "Biography" },
":yyyy": { N: 2013 },
},
}
let promise = docClient.query(params).promise();
promise.then(res => {
console.log("res:", res);
})
Run Code Online (Sandbox Code Playgroud)
我们可以使用containsin Filter 表达式来代替=.
所以,"info.genres = :genres"可以改为contains(info.genres , :gnOne)
在应用过滤器之前,AWS 仍将在分区键上进行查询,在单个查询中提取最多 1 MB 的数据。因此,我们将使用带有或不带有过滤器表达式的相同 RCU 来收费,但返回到客户端的数据量将受到限制,因此仍然有用。
const dynamodb = new AWS.DynamoDB();
dynamodb.query(
{
TableName: "my-test-table",
Limit: 20,
KeyConditionExpression: "id = :yyyy",
FilterExpression: `contains(info.genres , :gnOne)`,
ExpressionAttributeValues: {
":gnOne": { S: "Biography" },
":yyyy": { S: "2020" },
},
},
function (err, data) {
if (err) console.error(err);
else console.log("dynamodb scan succeeded:", JSON.stringify(data, null, 2));
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20831 次 |
| 最近记录: |