如何使用数组在 FilterExpression 中使用“IN"语句

编程入门 行业动态 更新时间:2024-10-24 14:25:01
本文介绍了如何使用数组在 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"语句

本文发布于:2023-11-28 07:46:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何使用   语句   FilterExpression   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!