Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search a value from array of objects in DynamoDB in node js

For suppose the item is like

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

}

then I want the output like

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

can you please suggest me the DynamoDB filter. Thanks in advance.

like image 468
RamMohan222 Avatar asked Oct 15 '25 04:10

RamMohan222


1 Answers

CONTAINS function can be used to filter or find the data in DynamoDB list. However, please note that you need to have both the attributes (i.e. id and subject) of the object (complex object in terms of DynamoDB) to find the data in List.

CONTAINS is supported for lists: When evaluating "a CONTAINS b", "a" can be a list; however, "b" cannot be a set, a map, or a list.

Sample code:-

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));
    }
});
like image 176
notionquest Avatar answered Oct 18 '25 03:10

notionquest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!