查询特定字段的 dynamodb [英] Query dynamodb for a specific field
问题描述
如何在 dynamodb 中查询匹配特定规则的所有项目?
how do I query dynamodb for all items that match a specific rule?
我正在建立一个在线商店(如 Asos),并且在 dynamodb 中我有一个产品表.所有产品都有类别"字段.所以我只想做一个简单的查询,我说
I am building an online shop (like Asos) and in dynamodb I have a products table. all of the products have the field 'category'. so I just want to do a simple query whereby I say something like
FROM products WHERE category == 'trainers'
我已经设法让它正常工作,所以我扫描了整个表格并将所有产品归还给我,但我现在想要更具体的查询
I have managed to get it working so I scan the whole table and return me all the products but i want more specific queries now
我假设我想使用类似的东西:KeyConditionExpression
但我在执行此操作时不断收到错误
I assume I want to use something like: KeyConditionExpression
but I keep getting an error when I do this
编辑的问题:
代码:
componentDidMount() {
console.log('mounted');
const AWS = window.AWS;
const params = {
TableName: 'Products',
FilterExpression: "category = trainers"
};
...secret key code
ddb.scan(params, (err, data) => {
if (err) {
console.log(err, err.stack);
} else {
console.log(data);
this.setState({ products: data })
}
});
推荐答案
试试这个
var params = {
ExpressionAttributeValues: {
":catval": {
S: "trainers"
}
},
FilterExpression: "category = :catval",
TableName: "Products"
};
dynamodb.scan(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
您不能在表达式中使用文字.您定义了一个 ExpressionAttributeValue,我将您的名称称为:catval",但您可以随意称呼它.'S' 表示一个字符串,所以基本上你定义了一个具有 String 类型和值trainers"的 ExpressionAttributeValue.然后,当您应用 FilterExpression 时,您将放入 ExpressionAttributeValue,而不是文字.
You can't use literals in expressions. You define an ExpressionAttributeValue, i've called yours ":catval" but you can call it anything you like. The 'S' means a string, so basically you define an ExpressionAttributeValue with type String and value "trainers". Then when you apply the FilterExpression you put in your ExpressionAttributeValue, not the literal.
这篇关于查询特定字段的 dynamodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!