0
votes

I am using JS SDK with DynamoDB to fetch data.

I am able to fetch data from my table using simple query with partition key and sort key.

My sort key sk has records -

Year#Batch#Rate

If I pass var sk = "2006#CSE#90"; it returns all of records matching this,

Requirement - How can I get all products with year 2006 , Batch CSE AND Rate =>90

readItem_pbro(){
  console.log("inside pbro");
  var table2 = "pbro";
  var pk = "1";
  var sk = "2006#CSE#90";

  var params2 = {
      TableName: table2,
      Key:{
          "pk": pk,
          "sk": sk
      }
  };

Edit 1 :: Created a different column for score/rate as score. It is numeric.

Now my query in JS is - but I am getting error - ValidationException: The provided key element does not match the schema

readItem_score_filter(){
  console.log("inside pbro");
  var table2 = "pbro"; 
  var pk = "1";  // string
  var sk = "2006#CSE"; // string
  var score = 90; //number

  var params2 = {
      TableName: table2,
      Key:{
          "pk": pk,
          "sk": sk,
          FilterExpression:'score >=:score',
      }
  };

what is wrong in my FilterExpression.

Edit 2 :: Added Key condition Expression but issue still remains the same

Error: ValidationException: The provided key element does not match the schema Here is my complete function now:

readItem_score_filter(){
  console.log("inside pbro");
  var table2 = "pbro";
  var pk = "1"; //string
  var sk = "2006#CSE"; // string
  var score = 90; //number

  var params2 = {
      TableName: table2,
      Key:{
          "pk": pk,
          "sk": sk,
          "score": score,
           KeyConditionExpression: 'pk = :pk AND sk=:sk',
           FilterExpression: "score >=:score",
      }
  };
  this.user.docClient.get(params2, function(err, data) {
    if (err) {
      console.log(err);
    } else {
      console.log(data);
    }
});
}

Screenshot of table attached incase you need to see::

enter image description here

1
"2006#CSE#90" - is this value of one attribute (column) or 3 attributes?Ravindra Bagale
this is sort key, one columnuser2828442
year , batch and rate are different columns or just one column?Ravindra Bagale
all in one columnuser2828442

1 Answers

0
votes

If "2006#CSE#90" this is the value of sort key column then you cant do anything at Dynamodb level..

comparings like this can be done through regular expressions but DynamoDB doesn't support Regular Expressions.

you simply need to get results and then seperate these values and compare ..

Updated :- use different column for score.

And use Filter Expression to get records having rate more than 90.

I dont know python , but still am trying here

var params2 = {
TableName: "pbro",
KeyConditionExpression: "pk = :pk AND sk =:sk", 
FilterExpression: "score >= :score" 

};