Dynamodb DB 扫描:过滤数组中的嵌入对象

Dynamodb DB scan: Filter on embedded object in array

尝试过滤看起来像这样的嵌入对象:

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

我正在尝试 return 所有引用类型为“HASHTAG”和值“#dynamodb”的帖子。我已经试过了,但它总是 returns null (运行 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);

并且只有 returns(成功)了一个空数组。

使用 contains function 筛选列表中的项目。在你的例子中,return 个帖子在 #references 指示的路径上有一个像 :map 的项目。

FilterExpression: 'contains(#references, :map)',
ExpressionAttributeNames: { '#references': 'references' },
ExpressionAttributeValues: {
  ':map': {
    type: 'HASHTAG',
    title: '#dynamodb',
  },
},