一尘不染

如何使用数组在FilterExpression中使用“ IN”语句-dynamodb

node.js

检查了AWS文档,但未找到任何有效示例。

这是我的尝试

var params = {
            TableName: "User",
            IndexName:"a-b-index",
            KeyConditionExpression: "Country = :country and #s = :status",
            FilterExpression: "Id IN (:e)",
            ExpressionAttributeValues: {
              ":country ": "USA",
              ":status": 1,
              ":e": "1"

            },
            ExpressionAttributeNames: {"#s": "Status"}
          };

          //get users
          dynamodb.query(params, function (err, data) {
            if (err)
              //error
            else {
              //success

            }
          });

有记录,但它正在获取具有ID的记录,1但我想使用这样的数组

 var params = {
        TableName: "User",
        IndexName:"a-b-index",
        KeyConditionExpression: "Country = :country and #s = :status",
        FilterExpression: "Id IN (:e)",
        ExpressionAttributeValues: {
          ":country ": "USA",
          ":status": 1,
          ":e": ["1","2","3"]

        },
        ExpressionAttributeNames: {"#s": "Status"}
      };

      //get users
      dynamodb.query(params, function (err, data) {
        if (err)
          //error
        else {
          //success

        }
       });

如何使上面的代码成为work.want以获取记录。语法正确,查询运行没有错误,但我没有记录


阅读 480

收藏
2020-07-07

共1个答案

一尘不染

请参考这个答案

摘要:-

对于“ IN”子句中的固定数量的值:

var params = {
    TableName : "Users",
    FilterExpression : "username IN (:user1, :user2)",
    ExpressionAttributeValues : {
        ":user1" : "john",
        ":user2" : "mike"
    }
};

对于数组中的更多元素并动态形成FilterExpression:-

var titleValues = ["The Big New Movie 2012", "The Big New Movie"];
var titleObject = {};
var index = 0;
titleValues.forEach(function(value) {
    index++;
    var titleKey = ":titlevalue"+index;
    titleObject[titleKey.toString()] = value;
});

var params = {
    TableName : "Movies",
    FilterExpression : "title IN ("+Object.keys(titleObject).toString()+ ")",
    ExpressionAttributeValues : titleObject
};
2020-07-07