I using node with the aws-sdk to try to remove an item (an object) from a nested list of objects in a dynamo db table if it exists but it seems more complicated than what I was hoping for.
I saw this response to a similar question earlier but they left out how they actually obtained the index that they are trying to remove
Add or remove an entry from a List type attribute in a DynamoDB table item
What I have looks like this:
{
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"
}
]
{
}
now lets say i want to remove this:
{
resource2Id: "12345",
name: "I want to remove this"
}
from the resource2 list
I think i need to do something like this:
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()
But im not sure how to get index_to_be_removed. Surely they dont expect me to do a separate query and search through the list myself? Seems there should be a way to tell them the id in the list to look for and remove it if it exists all in one call.
Unfortunately, DynamoDB doesn't provide a way to search for an index from within an UpdateExpression. This is something you'll need to do in your application code.
This is one drawback of using complex attributes to model one-to-many relationships.