如何在Dynamo DB中的运算符中使用 [英] how to use in operator in dynamo db

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

问题描述

我有一个带有用户名字段的用户表。我需要在dynamo db中写一些与此等效的东西:从用户中选择*,其中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.

推荐答案

应将各个用户指定为逗号分隔的String变量。 JavaScript数组等效于AWS DynamoDB数据类型中的List。 DynamoDB无法比较具有List属性(即JavaScript中的数组)的数据库中的String数据类型。

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

注意:-

我不认为具有数千个用户名的IN子句在性能方面不是一个好主意。

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

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

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