最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Query dynamodb for a specific field - Stack Overflow

programmeradmin6浏览0评论

how do I query dynamodb for all items that match a specific rule?

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

I assume I want to use something like: KeyConditionExpression but I keep getting an error when I do this

edited question:

code:

ponentDidMount() {
    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 })
  }
});

how do I query dynamodb for all items that match a specific rule?

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

I assume I want to use something like: KeyConditionExpression but I keep getting an error when I do this

edited question:

code:

ponentDidMount() {
    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 })
  }
});
Share Improve this question edited Jan 21, 2018 at 15:11 The Walrus asked Jan 21, 2018 at 8:50 The WalrusThe Walrus 1,2087 gold badges31 silver badges49 bronze badges 6
  • If you post your schema, the query you are currently trying and what you want to get, I can help. – F_SO_K Commented Jan 21, 2018 at 10:00
  • @Stu I have one table and I just want to pull stuff from it based on a field/column called category. and if there are 10 products, I only want the ones with the category == trainers to be returned. need any more? – The Walrus Commented Jan 21, 2018 at 15:00
  • You need to know your table schema in order to construct the query. As I say, if you can provide your schema and example code we can go from there. Otherwise there's nothing I can really add that the AWS documents don't already tell you. – F_SO_K Commented Jan 21, 2018 at 15:06
  • @Stu ok added, that is my table and some code so far where im trying to pull out just trainers so basically products 101 and 102 should be returned – The Walrus Commented Jan 21, 2018 at 15:12
  • OK, are you getting an error with this code? Whats the error message? – F_SO_K Commented Jan 21, 2018 at 15:15
 |  Show 1 more ment

2 Answers 2

Reset to default 2

Try this

 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);

EDIT: 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.

KeyConditionExpression is for when you want data filtered by primary key attributes. If that is not your case, you should use FilterExpression. A filter expression determines which items within the Scan results should be returned to you. All of the other results are discarded.

发布评论

评论列表(0)

  1. 暂无评论