简体   繁体   English

带有排序键和分区键的 DynamoDB 查询

[英]DynamoDB Query with sort key and partition key

I am using JS SDK with DynamoDB to fetch data.我正在使用带有 DynamoDB 的 JS SDK 来获取数据。

I am able to fetch data from my table using simple query with partition key and sort key .我可以使用带有partition keysort key的简单查询从我的表中获取数据。

My sort key sk has records -我的排序键sk有记录 -

Year#Batch#Rate

If I pass var sk = "2006#CSE#90";如果我通过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要求 -如何获得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.编辑 1::为分数/比率创建了一个不同的列作为分数。 It is numeric.它是数字的。

Now my query in JS is - but I am getting error - ValidationException: The provided key element does not match the schema现在我在JS中的查询是 - 但我收到错误 - 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 .我的FilterExpression出了什么问题。

Edit 2:: Added Key condition Expression but issue still remains the same编辑 2::添加了关键条件表达式,但问题仍然相同

Error: ValidationException: The provided key element does not match the schema Here is my complete function now:错误: ValidationException: The provided key element does not match the schema这是我现在完整的 function:

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::附表的屏幕截图以防您需要查看::

在此处输入图像描述

If "2006#CSE#90" this is the value of sort key column then you cant do anything at Dynamodb level..如果“2006#CSE#90”这是排序键列的值,那么你不能在 Dynamodb 级别做任何事情。

comparings like this can be done through regular expressions but DynamoDB doesn't support Regular Expressions.像这样的比较可以通过正则表达式完成,但 DynamoDB 不支持正则表达式。

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.并使用Filter Expression获取比率超过 90 的记录。

I dont know python, but still am trying here我不知道 python,但我仍然在这里尝试

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

}; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM