简体   繁体   English

如何在Dynamo DB中的运算符中使用

[英]how to use in operator in dynamo db

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'); 我需要在dynamo db中编写与此等效的内容:从用户中选择*,其中用户名in('a','b','c');

Adding more from code prosepective i have usernames in an array say var arr=['a','b','c']; 从预期的代码中添加更多内容我在数组中有用户名,例如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. 当我在数组中传递一个元素时,它会给我搜索用户表中传递的单个元素的结果,但是它不能与数组中的多个元素一起使用。

The individual users should be given as comma separated String variables. 各个用户应以逗号分隔的String变量形式给出。 JavaScript array is equivalent to List in AWS DynamoDB data type. JavaScript数组等效于AWS DynamoDB数据类型中的List。 The DynamoDB can't compare the String data type in database with List attribute (ie Array in JavaScript). DynamoDB无法将数据库中的String数据类型与List属性(即JavaScript中的Array)进行比较。

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

Construct the object from array for FilterExpression:- 从数组为FilterExpression构造对象:

Please refer the below code for forming the object dynamically based on Array value. 请参考以下代码,根据Array值动态形成对象。

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

Note:- 注意:-

I don't think IN clause with 1000s of usernames is a good idea in terms of performance. 我不认为具有数千个用户名的IN子句在性能方面不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM