【发布时间】:2021-01-13 14:20:38
【问题描述】:
数据库中有三个项目:
[
{
"year": 2013,
"info": {
"genres": ["Action", "Biography"]
}
},
{
"year": 2013,
"info": {
"genres": ["Crime", "Drama", "Thriller"]
}
},
{
"year": 2013,
"info": {
"genres": ["Action", "Adventure", "Sci-Fi", "Thriller"]
}
}
]
将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"]
}
};
var AWS = require("aws-sdk");
var docClient = new AWS.DynamoDB.DocumentClient();
let promise = docClient.query(params).promise();
promise.then(res => {
console.log("res:", res);
})
而不是匹配整个列表["Action", "Biography"],我宁愿进行查询以仅返回那些在存储在项目的info.genres 字段中的列表中包含字符串“传记”的表项目。我想知道这是否可能使用 DynamoDB query API?
稍后编辑。
工作解决方案(感谢 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);
})
【问题讨论】:
标签: javascript python typescript amazon-web-services amazon-dynamodb