NET C# - How to set a nested map in a list with a conditional expression by map value in dynamodb?

Viewed 253

The problem

We need to set a map value into a list, the problem is that we don't know how to make a reference to one of those values inside the map to do the check in a conditional expression, so we don't have any duplicates when data is coming.

The thing is that we're trying to do a check with "if_not_exists" inside a "list_append" without a get and the duplicate still comes, we've tried to do a

  1. "not contains"
  2. A search with an index inside a list for that value to do the check
  3. A comparison with <> structure

The update item structure

Some examples commented inline

var transactionData = new Dictionary<string, AttributeValue>
        {
             { "Id1", new AttributeValue { N = transacionDataModel.IdOne.ToString() } },
             { "Id2", new AttributeValue { S = transacionDataModel.IdTwo } },
             { "Price", new AttributeValue { N = transacionDataModel.Price.ToString() } },
             { "DataToCheckCondition", new AttributeValue { S = transacionDataModel.DataToCheck.ToString() } },
             { "Action", new AttributeValue { N = transacionDataModel.Action.ToString() } },
             { "Date", new AttributeValue { S = transacionDataModel.Date} }
        };

await dynamoDbClient.UpdateItemAsync(new UpdateItemRequest
        {
            Key = new Dictionary<string, AttributeValue>()
            {
                { "PartitionDate", new AttributeValue {
                      S = "20200821"
                  }
                },
                { "SortedIDKey", new AttributeValue {
                      N = "1"
                  }
                }
            },
            TableName = tableName,
            //UpdateExpression = "SET DataToSave= list_append(DataToSave, :newData)",
            //ConditionExpression = $"not contains(#DataToSave[0].#DataToCheckCondition, {transacionDataModel.Partida})",
            //UpdateExpression = "SET DataToSave= list_append(if_not_exists(DataToSave, :newData), :newData)",
            //ConditionExpression = $"not (contains(#DataToSave.#DataToCheckCondition, :localDataToCheck))",
            UpdateExpression = "SET #DataToSave = list_append(#DataToSave, :newData)",
            ConditionExpression = $"#DataToSave.#DataToCheckCondition <> :localDataToCheck",  
            ExpressionAttributeNames = new Dictionary<string, string>
            {
                { "#DataToSave", "DataToSave" },
                { "#DataToCheckCondition", "DataToCheckCondition" }
            },
            ExpressionAttributeValues =
            new Dictionary<string, AttributeValue>
            {
                { ":newData",
                    new AttributeValue
                    {
                        L = new List<AttributeValue>
                        {
                            new AttributeValue()
                            {
                                M = transactionData
                            }
                        }
                    }
                },
                {
                    ":localDataToCheck",
                    new AttributeValue
                    {
                        S = transacionDataModel.DataToCheck
                    }
                }
            }

        });
    }

Data structure

{
  "DataToSave": {
    "L": [
      {
        "M": {
          "Action": {
            "N": "1"
          },
          "Date": {
            "S": "25/08/2020 8:31:48"
          },
          "Id1": {
            "N": ""
          },
          "Id2": {
            "S": "d"
          },
          "DataToCheckCondition": {
            "S": "151"
          },
          "Price": {
            "N": "101"
          }
        }
      }
    ]
  },
  "PartitionDate": {
    "S": "20200821"
  },
  "SortedIDKey": {
    "N": "1"
  }
}
0 Answers
Related