Jose A
Jose A

Reputation: 11077

Filter Expression based on a nested object DynamoDB AppSync

I'm trying to filter out a query based on a nested object (no array). I'm currently using AppSync and DynamoDB and the expression with expression values are executed correctly. But the filtering doesn't seem to work.

This is the sample data I'm trying to get (Filter by indicator.id): enter image description here

Here's my query:

{
  "version": "2017-02-28",
  "operation": "Query",
  "query": {
    "expression": "pk = :pk and begins_with(sk, :sk)",
    "expressionValues": {
      ":pk": { "S": "tenant:5fc30406-346c-42e2-8083-fda33ab6000a" },
      ":sk": {
        "S": "school-year:2019-2020:grades:bVgA9abd:subject:m_kpc1Ae6:indicator:"
      }
    }
  },
  "filter": {
    "expression": " contains(#indicatorId, :sk1) or contains(#indicatorId, :sk2) or contains(#indicatorId, :sk3)",
    "expressionNames": { "#indicatorId": "indicator" },
    "expressionValues": {
      ":sk1": {
        "M": { "id": { "S": "07c658dd-999f-4e6f-95b8-c6bae422760a" } }
      },
      ":sk2": {
        "M": { "id": { "S": "0cf9f670-e284-4a93-b297-5e4a40c50228" } }
      },
      ":sk3": { "M": { "id": { "S": "cd7902be-6512-4b47-b29d-40aff30c73e6" } } }
    }
  }
}

I've also tried:

{
  "version": "2017-02-28",
  "operation": "Query",
  "query": {
    "expression": "pk = :pk and begins_with(sk, :sk)",
    "expressionValues": {
      ":pk": { "S": "tenant:5fc30406-346c-42e2-8083-fda33ab6000a" },
      ":sk": {
        "S": "school-year:2019-2020:grades:bVgA9abd:subject:m_kpc1Ae6:indicator:"
      }
    }
  },
  "filter": {
    "expression": " contains(#indicatorId, :sk1) or contains(#indicatorId, :sk2) or contains(#indicatorId, :sk3)",
    "expressionNames": { "#indicatorId": "indicator.id" },
    "expressionValues": {
      ":sk1": { "S": "07c658dd-999f-4e6f-95b8-c6bae422760a" },
      ":sk2": { "S": "0cf9f670-e284-4a93-b297-5e4a40c50228" },
      ":sk3": { "S": "cd7902be-6512-4b47-b29d-40aff30c73e6" } 
    }
  }
}

I've also tried searching around StackOverflow, and Amazon forums and haven't found it directly to my problem: How to filter by elements in an array (or nested object) in DynamoDB Nested Query in DynamoDB returns nothing

Upvotes: 0

Views: 1098

Answers (1)

Sunny
Sunny

Reputation: 176

Referring to this answer.enter link description here

According to DDB Nested Attributes doc, the filter expression should look like the following format:

"filter" : {
        "expression" : "#path.#filter = :${fp}",  ## filter path parent.target = :target
        "expressionNames": {
            "#path" : "${path}",
            "#filter" : "${fp}"
        },
        "expressionValues" : {
            ":${fp}" : $util.dynamodb.toDynamoDBJson(${$target[$fp].eq}) ## :target : value to filter for
        }
}

Upvotes: 1

Related Questions