混淆创建DynamoDB查询 [英] Confused creating a DynamoDB query
问题描述
我在dynamo DB中拥有以下数据
I'm having the below data in dynamo DB
id category value rule
1 a 5 1
2 b 5 1
3 c 5 1
4 a 5 2
5 b 5 2
6 a 2 3
7 b 2 3
8 a 3 4
9 b 3 4
10 c 2 4
11 d 2 4
12 b 5 5
13 c 5 5
这里我的目标如下.
- 首先扫描表并获取大小为
2
的rule
.一旦获得该规则,请检查其类别中是否只有a
和b
.忽略其余部分.
- First scan the table and get the
rule
with size2
. once I get that rule, check if it has onlya
andb
in category. Ignore the rest.
这是我当前的查询.
var array=["a","b"];
for (index = 1; index <= 5; ++index) {
console.log(index);
var params1 = {
TableName: "MyCatTable",
FilterExpression: "#rule=:rule",
ExpressionAttributeNames: {
"#rule": "rule"
},
ExpressionAttributeValues: {
":rule": String(index)
}
};
results.push(dynamodb.scan(params1).promise().then(function (data) {
if (data.Items.length == 2 && (array.indexOf(data.Items[0].category) >= 0)) {
var uw = data.Items;
return uw;
}
}));
}
return Promise.all(results);
}).then((data) => {
var res;
console.log("----------------------");
console.log(data);
})
运行此命令时,它会返回规则2
,3
和5
.但是我只需要2
和3
.因为5
具有b
和c
而不是a
和b
.
when I run this it is returning me the rules 2
, 3
and 5
. But I need only 2
and 3
. since 5
has b
and c
instead of a
and b
.
或者还有其他方法可以过滤掉我得到的json响应吗?
or is there any other way of filtering out the json response that I get?
请让我知道我要去哪里哪里以及如何解决这个问题.
please let me know where am I going wrong and how can I fix this.
谢谢
推荐答案
问题出在这部分代码上:
The issue is with this part of the code:
data.Items[0].category
它仅检查"data.Items"数组中第一个元素的类别,您需要通过检查每个元素的类别而不是仅第一个元素来过滤该数组.返回5的原因是因为5的第一个元素是数组中存在的'b'.
it only checks the category of the first element on the "data.Items" array, you need to filter the array by checking the category of each element instead of just the first one. The reason that 5 is returned is because the first element of 5 is 'b' which exists in your array.
*已编辑-根据要求的代码片段(这应该是代码外观的粗略轮廓.不过可能会有一些错误)
*edited - code fragment as requested (this should be a rough outline of how the code should look. there might be a few errors though)
if (data.Items.length == 2) {
var uw = [];
for(var x in data.Items){
if(array.indexOf(x.category) >= 0) uw.push(x);
}
return uw;
}
这篇关于混淆创建DynamoDB查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!