如何动态替换Dynamodb中的条件参数? [英] How can I dynamically substitute where condition parameters in Dynamodb?
问题描述
当前代码示例:
Scan * from table_name where name='ajith' and lastname='gupta';
在实际情况下,我将获得不同的值。我需要在上面的查询中传递这些参数并获取输出。另外,最好防止数据注入。
In the real scenario, I will be getting different values. I need to pass those here in this above query and get the output. Also, it's better to prevent data injection.
所需:
Scan * from table_name where name=? and lastname=?;
推荐答案
使用表达式属性名称和表达式属性值参数。
Use the Expression Attribute Names and Expression Attribute Values parameters.
使用DocumentClient的Javascript示例:
Javascript example using the DocumentClient:
let params = {
TableName: 'table_name',
KeyConditionExpression: '#lname = :lname and #name = :name',
ExpressionAttributeNames: {
'#name': 'name',
'#lname': 'lastname'
},
ExpressionAttributeValues: {
':name': 'ajith',
':lname': 'qupta',
}
}
dynamodbDocumentClient.query(params).promise().then(result => {
// soSomethingWithTheResultHere
})
此示例假定您有一个表,该表的分区和排序键名为 name
和姓氏
参见 https://docs.aws.amazon.com/amazondynamodb/latest/deve loperguide / Query.html 有关查询的更多信息。
This example assumes you have a table with partition and sort key of called name
and lastname
See https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html for more information about queries.
这篇关于如何动态替换Dynamodb中的条件参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!