基于嵌套对象DynamoDB AppSync的过滤器表达式

时间:2019-09-16 19:22:49

标签: amazon-dynamodb aws-appsync

我正在尝试基于嵌套对象(无数组)过滤掉查询。我当前正在使用AppSync和DynamoDB,带有表达式值的表达式已正确执行。但是过滤似乎无效。

这是我要获取的示例数据(按indicator.id过滤): enter image description here

这是我的查询:

{
  "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" } } }
    }
  }
}

我也尝试过:

{
  "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" } 
    }
  }
}

我也尝试过搜索StackOverflow和Amazon论坛,但没有直接找到我的问题: How to filter by elements in an array (or nested object) in DynamoDB Nested Query in DynamoDB returns nothing

1 个答案:

答案 0 :(得分:1)

请参阅此答案。enter link description here

根据DDB Nested Attributes doc,过滤器表达式应类似于以下格式:

"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
        }
}