5

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:

enter image description here

code:

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 })
  }
});
The Walrus
  • 1,148
  • 6
  • 28
  • 46
  • If you post your schema, the query you are currently trying and what you want to get, I can help. – F_SO_K Jan 21 '18 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 Jan 21 '18 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 Jan 21 '18 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 Jan 21 '18 at 15:12
  • OK, are you getting an error with this code? Whats the error message? – F_SO_K Jan 21 '18 at 15:15
  • no error, my console.log just shows > `{Items: Array(0), Count: 0, ScannedCount: 3}` basically saying it scanned the 3 items and didn't find any matches I guess. have I done the filter correctly ? – The Walrus Jan 21 '18 at 15:18

2 Answers2

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.

F_SO_K
  • 13,640
  • 5
  • 54
  • 83
1

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.

Deividi Silva
  • 816
  • 8
  • 13