RFE
RFE

Reputation: 25

Nested attributes query on DynamoDB

I have something like this on Dynamo:

{
    "mail": "[email protected]",
    "data": {
        "type": 1
    }
}

I have an index on "mail" attribute and I'm trying to query over all data found with an specified mail filtering the attribute "data". Something like this:

const params = {
    TableName: 'tableName',
    IndexName: "mail_index",
    KeyConditionExpression: "#mail = :mail",
    FilterExpression: '#status = :val',
    ExpressionAttributeNames: {
        '#mail': 'mail',
        '#status': 'data.type'
    },
    ExpressionAttributeValues: {
        ':mail': '[email protected]',
        ':val': {N: 5}
    } 
};

dynamoDoc.query(params, (err, data) => {
    console.log(data);
});

But I'm always getting an empty result. What am I doing wrong?

Upvotes: 0

Views: 234

Answers (1)

Milan Cermak
Milan Cermak

Reputation: 8064

Try this:

const params = {
    TableName: 'tableName',
    IndexName: "mail_index",
    KeyConditionExpression: "#mail = :mail",
    FilterExpression: '#data.#type = :val',
    ExpressionAttributeNames: {
        '#mail': 'mail',
        '#data': 'data',
        '#type': 'type'
    },
    ExpressionAttributeValues: {
        ':mail': '[email protected]',
        ':val': {N: 5}
    } 
};

Because both data and type are reserved words, DynamoDB needs both of them to be escaped.

Upvotes: 2

Related Questions