Search code examples
.netlistamazon-web-servicesamazon-dynamodbupdating

AWS DynamoDb - Updating a specific map in list (.NET)


I have an attribute called "Categories" which contains a list of maps. I would like to be able to update just a single one of these maps using its unique id. I have structured my data as follows:

"EstablishmentID": "12345abc",
"Categories": [
{
  "408eec12-2415-45d3-b703-b8ba0d55261c": {        
    "CategoryName": "Cat11",        
    "CategoryVisible": true
  }
},
{
  "586a95ab-e39d-4711-8770-f60dedcf6059": {    
    "CategoryName": "Cat2",       
    "CategoryVisible": true
  }
}
]

When I want to update one of these categories, I use a SET update expression and (to my knowledge) I am inputting the document path correctly. Here is my code for the update expression:

UpdateItemRequest request = new UpdateItemRequest
        {
            TableName = "Menu",
            Key = new Dictionary<string, AttributeValue> { { "EstablishmentID", new AttributeValue { S = establishmentID.ToString() } } },
            ExpressionAttributeNames = new Dictionary<string, string>
            {
                {"#C", "Categories"},
                {"#I", category.CategoryID.ToString()}
            },
            ExpressionAttributeValues = new Dictionary<string, AttributeValue>
            {
                {":CategoryToUpdate", new AttributeValue{M=values } }
            },
            UpdateExpression = "SET #C.#I = :CategoryToUpdate"

        };

When I try to run my update I get the following error:

The document path provided in the update expression is invalid for update

To me this seems that is not able to find the object Categories => [categoryID]. Does anyone know what I might be missing with regards to being able to find my specific category in the list.

Thanks in advance. Lee


Solution

  • You can't use a list and query like this. #C.#I would be referencing Categories.408eec12-2415-45d3-b703-b8ba0d55261c. Change your format to this:

    "EstablishmentID": "12345abc",
    "Categories": {
      "408eec12-2415-45d3-b703-b8ba0d55261c": {        
        "CategoryName": "Cat11",        
        "CategoryVisible": true
      },
      "586a95ab-e39d-4711-8770-f60dedcf6059": {    
        "CategoryName": "Cat2",       
        "CategoryVisible": true
      }
    }
    

    Then your query should work.