admin管理员组文章数量:1026989
I have a DynamoDB table where I store this data;
JSON data
where "device" is the partition key and "datetime" the sort key. I'm able to query data based on PartitionKey and range of dates (sort key). But now, I would need to filter based on "endpoint" which is inside "data" which is an array. For example, I would like to retrieve the data only if endpoint 1 exist in data array, or for example if endpoint IN (1,3) exist. I 'm using following code;
var dev = event.params.querystring.device;
var from = event.params.querystring.from;
var to = event.params.querystring.to;
var ascord = event.params.querystring.ascord;
var limit = event.params.querystring.limit;
var qryParams = {
TableName : "mytable",
KeyConditionExpression: "#id = :dddd and #tm between :time1 and :time2",
FilterExpression: "data.endpoint = :ep",
ExpressionAttributeNames:{
"#id": "device",
"#tm": "datetime"
},
ExpressionAttributeValues: {
":dddd": dev,
":time1": from,
":time2": to,
":ep": 1
}
};
docClient.query(qryParams, function(err, data){
if (err){
callback(err, null);
}else {
callback(null, data);
}
})
I have a DynamoDB table where I store this data;
JSON data
where "device" is the partition key and "datetime" the sort key. I'm able to query data based on PartitionKey and range of dates (sort key). But now, I would need to filter based on "endpoint" which is inside "data" which is an array. For example, I would like to retrieve the data only if endpoint 1 exist in data array, or for example if endpoint IN (1,3) exist. I 'm using following code;
var dev = event.params.querystring.device;
var from = event.params.querystring.from;
var to = event.params.querystring.to;
var ascord = event.params.querystring.ascord;
var limit = event.params.querystring.limit;
var qryParams = {
TableName : "mytable",
KeyConditionExpression: "#id = :dddd and #tm between :time1 and :time2",
FilterExpression: "data.endpoint = :ep",
ExpressionAttributeNames:{
"#id": "device",
"#tm": "datetime"
},
ExpressionAttributeValues: {
":dddd": dev,
":time1": from,
":time2": to,
":ep": 1
}
};
docClient.query(qryParams, function(err, data){
if (err){
callback(err, null);
}else {
callback(null, data);
}
})
But it doesn't retrieve any record. I also tried to use placeholders (#data.#endpoint) but didn't work. What would be the right way to do it? Also, should I create an index in the table for the endpoints?
Thanks for your help. Regards; Gus
Share Improve this question asked Aug 25, 2017 at 3:59 Gus SabinaGus Sabina 3112 silver badges15 bronze badges1 Answer
Reset to default 3The attribute data
is a list of objects. So, DynamoDB can't find the item if you don't provide the array index of the list attribute. You can use contains
function, if you know the value of endpoint
and value
.
Option 1 - Using contains:-
var params = {
TableName: table,
KeyConditionExpression: "#id = :dddd and #tm between :time1 and :time2",
FilterExpression: "contains (#data, :ep)",
ExpressionAttributeNames: {
"#id": "deviceid",
"#tm": "datetime",
"#data" : "data"
},
ExpressionAttributeValues: {
":dddd": "1",
":time1": "2017-08-22T00:44:11:424Z",
":time2": "2017-08-22T23:44:11:424Z",
":ep": {
"endpoint": 1,
"value": "23"
}
}
};
Option 2 - Using Index :-
var params = {
TableName: table,
KeyConditionExpression: "#id = :dddd and #tm between :time1 and :time2",
FilterExpression: "#data"+"[0].endpoint = :ep",
ExpressionAttributeNames: {
"#id": "deviceid",
"#tm": "datetime",
"#data" : "data"
},
ExpressionAttributeValues: {
":dddd": "1",
":time1": "2017-08-22T00:44:11:424Z",
":time2": "2017-08-22T23:44:11:424Z",
":ep": 1
}
};
I have a DynamoDB table where I store this data;
JSON data
where "device" is the partition key and "datetime" the sort key. I'm able to query data based on PartitionKey and range of dates (sort key). But now, I would need to filter based on "endpoint" which is inside "data" which is an array. For example, I would like to retrieve the data only if endpoint 1 exist in data array, or for example if endpoint IN (1,3) exist. I 'm using following code;
var dev = event.params.querystring.device;
var from = event.params.querystring.from;
var to = event.params.querystring.to;
var ascord = event.params.querystring.ascord;
var limit = event.params.querystring.limit;
var qryParams = {
TableName : "mytable",
KeyConditionExpression: "#id = :dddd and #tm between :time1 and :time2",
FilterExpression: "data.endpoint = :ep",
ExpressionAttributeNames:{
"#id": "device",
"#tm": "datetime"
},
ExpressionAttributeValues: {
":dddd": dev,
":time1": from,
":time2": to,
":ep": 1
}
};
docClient.query(qryParams, function(err, data){
if (err){
callback(err, null);
}else {
callback(null, data);
}
})
I have a DynamoDB table where I store this data;
JSON data
where "device" is the partition key and "datetime" the sort key. I'm able to query data based on PartitionKey and range of dates (sort key). But now, I would need to filter based on "endpoint" which is inside "data" which is an array. For example, I would like to retrieve the data only if endpoint 1 exist in data array, or for example if endpoint IN (1,3) exist. I 'm using following code;
var dev = event.params.querystring.device;
var from = event.params.querystring.from;
var to = event.params.querystring.to;
var ascord = event.params.querystring.ascord;
var limit = event.params.querystring.limit;
var qryParams = {
TableName : "mytable",
KeyConditionExpression: "#id = :dddd and #tm between :time1 and :time2",
FilterExpression: "data.endpoint = :ep",
ExpressionAttributeNames:{
"#id": "device",
"#tm": "datetime"
},
ExpressionAttributeValues: {
":dddd": dev,
":time1": from,
":time2": to,
":ep": 1
}
};
docClient.query(qryParams, function(err, data){
if (err){
callback(err, null);
}else {
callback(null, data);
}
})
But it doesn't retrieve any record. I also tried to use placeholders (#data.#endpoint) but didn't work. What would be the right way to do it? Also, should I create an index in the table for the endpoints?
Thanks for your help. Regards; Gus
Share Improve this question asked Aug 25, 2017 at 3:59 Gus SabinaGus Sabina 3112 silver badges15 bronze badges1 Answer
Reset to default 3The attribute data
is a list of objects. So, DynamoDB can't find the item if you don't provide the array index of the list attribute. You can use contains
function, if you know the value of endpoint
and value
.
Option 1 - Using contains:-
var params = {
TableName: table,
KeyConditionExpression: "#id = :dddd and #tm between :time1 and :time2",
FilterExpression: "contains (#data, :ep)",
ExpressionAttributeNames: {
"#id": "deviceid",
"#tm": "datetime",
"#data" : "data"
},
ExpressionAttributeValues: {
":dddd": "1",
":time1": "2017-08-22T00:44:11:424Z",
":time2": "2017-08-22T23:44:11:424Z",
":ep": {
"endpoint": 1,
"value": "23"
}
}
};
Option 2 - Using Index :-
var params = {
TableName: table,
KeyConditionExpression: "#id = :dddd and #tm between :time1 and :time2",
FilterExpression: "#data"+"[0].endpoint = :ep",
ExpressionAttributeNames: {
"#id": "deviceid",
"#tm": "datetime",
"#data" : "data"
},
ExpressionAttributeValues: {
":dddd": "1",
":time1": "2017-08-22T00:44:11:424Z",
":time2": "2017-08-22T23:44:11:424Z",
":ep": 1
}
};
本文标签: javascriptFilter expression based on a nested attribute in an arrayDynamoDBStack Overflow
版权声明:本文标题:javascript - Filter expression based on a nested attribute in an array - DynamoDB - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745662986a2162003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论