Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Filter Nested Array Object in DynamoDB

I am very beginner to AWS DynamoDB, I want to scan the DynamoDB with SENDTO.emailAddress = "[email protected]" as FilterExpression.

The DB Structure looks like this

{
    ID
    NAME
    MESSAGE
    SENDTO[
        {
            name
            emailAddress
        }
    ]
}

A Sample Data

{
    ID: 1,
    NAME: "HELLO",
    MESSAGE: "HELLO WORLD!",
    SENDTO: [
        {
            name: "First",
            emailAddress: "[email protected]"
        },
        {
            name: "Second",
            emailAddress: "[email protected]"
        }
    ]
}

I want to retrieve document that match emailAddress. I tried to scan with filter expression and here is my code to retrieve the data. I am using AWS Javascript SDK.

let params = {
    TableName : "email",
    FilterExpression: "SENDTO.emailAddress = :emailAddress",
    ExpressionAttributeValues: {
        ":emailAddress": "[email protected]",
    }
}

let result = await ctx.docClient.scan(params).promise();
like image 802
Narayanan Ramanathan Avatar asked Nov 14 '17 19:11

Narayanan Ramanathan


People also ask

Does DynamoDB support nested objects?

In addition to simple data types like numbers and strings DynamoDB supports these types: Nested object: A value of an attribute in DynamoDB can be a complex nested object. Set: A set of numbers, strings, or binary values. List: An untyped list that can contain any values.

What is the difference between Scan and Query in DynamoDB?

DynamoDB supports two types of read operations: Query and Scan. To find information, a query operation uses either the primary key or the index key. Scan, as the name implies, is a read call that scans the entire table for a specified result. DynamoDB is designed to be query-optimized.

Can you Query JSON in DynamoDB?

So this query operation isn't supported by DynamoDB at this time. You might want to consider other NoSQL document based databases which are more flexible like MongoDB Atlas, if you need this kind of querying functionality.

Can you Query on sort key DynamoDB?

You can not query only using a Sort Key. You need to specify a partition key to perform query operations. Else, you need to create a global secondary index or perform a scan operation.


1 Answers

In order to find the item by sendto attribute, you need to know both name and emailAddress attribute value. DynamoDB can't find the data by just one of the attributes in an object (i.e. email attribute value alone).

CONTAINS function can be used to find the data in List data type.

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 using Contains:-

var params = {
    TableName: "email",
    FilterExpression: "contains (SENDTO, :sendToVal)",
    ExpressionAttributeValues: {
        ":sendToVal": {
            "name" : "First",
            "emailAddress" : "[email protected]"
        }
    }
}; 

If you don't know the value of name and emailAddress attribute, you may need to remodel the data to fulfill your use case.

like image 93
notionquest Avatar answered Sep 20 '22 23:09

notionquest