0

Trying to be able to filter on embedded object that looks like:

    "posts": [
        {
            "id": "10e85cf7-acd2-417b-a5dc-1dfb6de606bf",
            "references": [
                {
                    "type": "URL",
                    "title": "How to get dynamodb to only return certain columns",
                },
                {
                    "type": "HASHTAG",
                    "title": "#dynamodb",
                },
            ]
        },
...
]

I am trying to return all posts that have reference of type "HASHTAG" and VALUE "#dynamodb". I have tried this but it always returns null (running in node.js):

const params = {
  TableName: "tableName",
  ScanIndexForward: false,
  FilterExpression: "#references.#type = :referenceValue",
  ExpressionAttributeNames: {
    "#references": "references",
    "#type": "HASHTAG"
  },
  ExpressionAttributeValues: {
      ":referenceValue": "#dynamoDB"
  }
}
const response = await docClient.scan(params).promise();
console.log(response);

And only returns (successfully) an empty array.

1 Answer 1

2

Use the contains function to filter on items in a list. In your case, return posts that have an item like :map at the path indicated by #references.

FilterExpression: 'contains(#references, :map)',
ExpressionAttributeNames: { '#references': 'references' },
ExpressionAttributeValues: {
  ':map': {
    type: 'HASHTAG',
    title: '#dynamodb',
  },
},
Sign up to request clarification or add additional context in comments.

3 Comments

Actually a follow-up question: imagining I had a more complex object with more fields than just "title" (e.g. "text"), is it possible to check if the type & title match and ignore all other fields? Looking at documentation it seemed maybe not.
@Andyrewwer You are right, no partial or wildcard matching. The map either matches or doesn't. The best you can do to combine multiple contains functions with an OR operator.
@Andyrewwer Also, remember a scan with a filter is still a scan. The filter is applied after reading all records. This means that having to further filter the results client-side is probably not adding much DynamoDB expense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.