如何在发电机数据库中使用 in 运算符 [英] how to use in operator in dynamo db

查看:9
本文介绍了如何在发电机数据库中使用 in 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有字段用户名的用户表.我需要在 dynamo db 中编写与此等效的内容: Select * from user where username in('a','b','c');

I have a user table with a field username. I need to write something equivalent to this in dynamo db: Select * from user where username in('a','b','c');

从代码中添加更多我在数组中有用户名说 var arr=['a','b','c'];

Adding more from code prosepective i have usernames in an array say var arr=['a','b','c'];

到目前为止,我尝试过这个结果为零

I so far tried this which is giving me zero result

    this.dynamo.client.scanAsync({
        TableName: this.dynamo.table('users'),
        FilterExpression: 'username IN (:list)',
        ExpressionAttributeValues: {
            ':list': arr.toString()
        }
    }).then((response) => {
        console.log(response);
        return {
            userFriends: result.Item.friends
        };
    });

当我在数组中传递一个元素时,它会给我结果搜索在用户表中传递的单个元素,但它不能与数组中的多个元素一起使用.

When I pass one element in array it give me result searching passed single element in user table but its not working with more than one element in array.

推荐答案

各个用户应以逗号分隔的字符串变量形式给出.JavaScript 数组相当于 AWS DynamoDB 数据类型中的 List.DynamoDB 无法将数据库中的 String 数据类型与 List 属性(即 JavaScript 中的数组)进行比较.

The individual users should be given as comma separated String variables. JavaScript array is equivalent to List in AWS DynamoDB data type. The DynamoDB can't compare the String data type in database with List attribute (i.e. Array in JavaScript).

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

为FilterExpression从数组构造对象:-

请参考以下代码,根据数组值动态形成对象.

Please refer the below code for forming the object dynamically based on Array value.

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

注意:-

我认为包含 1000 个用户名的 IN 子句在性能方面不是一个好主意.

I don't think IN clause with 1000s of usernames is a good idea in terms of performance.

这篇关于如何在发电机数据库中使用 in 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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