How to use “IN” statement in FilterExpression using array - dynamodb

前端 未结 6 2067
猫巷女王i
猫巷女王i 2020-12-08 15:58

Checked AWS document but did not find any working example.

Here is my attempt

var params = {
            TableName: \"User\",
            IndexName:\         


        
6条回答
  •  甜味超标
    2020-12-08 16:20

    notionquest's answer is correct but i cant use my other values in ExpressionAttributeValues like :country and :status so here is modified answer to make it working as per my requirements

    var AttributeValuesObject = {};
    
      AttributeValuesObject[':country '] = "USA";
      AttributeValuesObject[':status'] = 1;
    
      var titleValues = ["1", "2"];
      var titleObject = {}; 
    
      var index = 0; 
    
      titleValues.forEach(function(value) {
        index++;
        var titleKey = ":titleValue"+index;
        AttributeValuesObject[titleKey.toString()] = value;
        titleObject[titleKey.toString()] = value;
      });
    
      var params = {
        TableName: "User",
        IndexName:"a-b-index",
        KeyConditionExpression: "Country = :country and #s = :status",
        FilterExpression: "Id IN ("+Object.keys(titleObject).toString()+ ")",
        ExpressionAttributeValues: AttributeValuesObject,
        ExpressionAttributeNames: {"#s": "Status"}
      };
    
      //get users
      dynamodb.query(params, function (err, data) {
        if (err)
          //error
        else {
          //success
    
        }
       });
    

提交回复
热议问题