如何使用数组在 FilterExpression 中使用“IN"语句 - dynamodb [英] How to use “IN” statement in FilterExpression using array - dynamodb

查看:33
本文介绍了如何使用数组在 FilterExpression 中使用“IN"语句 - dynamodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查了 AWS 文档,但没有找到任何工作示例.

Checked AWS document but did not find any working example.

这是我的尝试

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 的记录,但我想使用这样的数组

Got records but it is fetching record which have id 1 but i want to use array like this

 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

        }
       });

如何使上面的代码有效.想要获取记录.语法正确,查询运行没有错误,但我没有得到记录

How can make above code as working.want to get records. syntax is correct and query run without error but i am not getting records

推荐答案

请参考这个答案

总结:-

对于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
};

这篇关于如何使用数组在 FilterExpression 中使用“IN"语句 - dynamodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆