【问题标题】:Filter Expression based on a nested object DynamoDB AppSync基于嵌套对象 DynamoDB AppSync 的筛选表达式
【发布时间】:2020-01-17 16:00:30
【问题描述】:

我正在尝试过滤掉基于嵌套对象(无数组)的查询。我目前正在使用 AppSync 和 DynamoDB,并且具有表达式值的表达式已正确执行。但过滤似乎不起作用。

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

这是我的查询:

{
  "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 和亚马逊论坛上搜索,但没有直接找到我的问题: How to filter by elements in an array (or nested object) in DynamoDB Nested Query in DynamoDB returns nothing

【问题讨论】:

    标签: amazon-dynamodb aws-appsync


    【解决方案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
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      相关资源
      最近更新 更多