从 dynamo db table 的嵌套列表中删除对象(如果存在)

Remove object from dynamo db table's nested list if it exists

我将 node 与 aws-sdk 结合使用,尝试从 dynamo 数据库 table 的嵌套对象列表中删除一个项目(一个对象),如果它存在,但它似乎比我原来的更复杂希望。

我早些时候看到了对类似问题的回复,但他们没有说明他们实际上是如何获得他们试图删除的索引的

我得到的是这样的:

{
  id: "1",
  resources: {
    resource1: [
      {
        resource1Id: "63236",
        name: "This is the first resource1 resource"
      }
    ],
    resource2: [
      {
        resource2Id: "63236",
        name: "This is the first resource2 resource"
      },
      {
        resource2Id: "12345",
        name: "I want to remove this"
      }
    ]
  {
}

现在假设我想删除这个:

{
  resource2Id: "12345",
  name: "I want to remove this"
}

来自 resource2 列表

我想我需要做这样的事情:

    query = "REMOVE resources.resource2[%d]" % (index_to_be_removed)

    const updateParams = {
      TableName: myTable,
      Key: {
        "id": myId
      },
      "UpdateExpression": "query",
      "ReturnValues": "UPDATED_NEW"
    }
    return await this.dynamodbClient.update(updateParams).promise()

但我不确定如何获得 index_to_be_removed。他们肯定不希望我自己进行单独的查询并搜索列表吗?似乎应该有一种方法可以告诉他们列表中的 id 以查找并删除它(如果它存在于一次调用中)。

遗憾的是,DynamoDB 不提供从 UpdateExpression 中搜索索引的方法。这是您需要在应用程序代码中执行的操作。

这是使用复杂属性建模 one-to-many 关系的缺点之一。