简体   繁体   English

根据数组中的嵌套属性过滤表达式 - DynamoDB

[英]Filter expression based on a nested attribute in an array - DynamoDB

I have a DynamoDB table where I store this data; 我有一个DynamoDB表,我存储了这些数据;

JSON data JSON数据

where "device" is the partition key and "datetime" the sort key. 其中“device”是分区键,“datetime”是排序键。 I'm able to query data based on PartitionKey and range of dates (sort key). 我能够根据PartitionKey和日期范围(排序键)查询数据。 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. 例如,我想仅在端点1存在于数据阵列中时检索数据,或者例如,如果存在端点IN(1,3)。 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. 我也试过使用占位符(#data。#endpoint)但是没有用。 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 格斯

The attribute data is a list of objects. 属性data是对象列表。 So, DynamoDB can't find the item if you don't provide the array index of the list attribute. 因此,如果您不提供list属性的数组索引,DynamoDB将无法找到该项。 You can use contains function, if you know the value of endpoint and value . 如果您知道endpointvalue的值,则可以使用contains函数。

Option 1 - Using contains:- 选项1 - 使用包含: -

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 :- 选项2 - 使用指数: -

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

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

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