如何在节点js中的DynamoDB中搜索对象数组中的值

时间:2017-11-02 13:21:48

标签: amazon-dynamodb

假设该项目类似于

{
'EventType': 'git/push'
'EventTime': 1416251010,
'Commits': [
    {
        'id': '29d02aff...',
        'subject': 'Add the thing to the place'
    },
    {
        'id': '9d888fec...',
        'subject': 'Spelling errors'
    },
    ...
]

}

然后我希望输出像

{
'EventType': 'git/push'
'EventTime': 1416251010,
'Commits': [
    {
        'id': '29d02aff...',
        'subject': 'Add the thing to the place'
    }
 ]
}

您可以向我推荐DynamoDB过滤器吗? 提前谢谢。

1 个答案:

答案 0 :(得分:3)

CONTAINS 函数可用于过滤或查找DynamoDB列表中的数据。但是,请注意,您需要同时拥有对象的属性(即id和subject)(根据DynamoDB的复杂对象)才能在List中查找数据。

  

列表支持CONTAINS:评估" a CONTAINS b"," a"   可以是一个清单;然而," b"不能是集合,地图或列表。

示例代码: -

var table = "eventtype";
var params = {
    TableName: table,
    KeyConditionExpression: "EventType = :eventType",
    FilterExpression: "contains(Commits, :commitVal )",
    ExpressionAttributeValues: {
        ":eventType": 'git/push',
        ":commitVal": {
            'id': '29d02aff',
            'subject': 'Add the thing to the place'
        }
    }
};

docClient.query(params, function (err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err,
            null, 2));
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
});