Reputation: 36583
This is pretty much straight out of the documentation (http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html), but like most things AWS, it just doesn't work and isn't documented well at all.
The following fails with "The document path provided in the update expression is invalid for update"
var messageId = "f244ed33-d678-4763-8f46-0f06514d5139"
var sequenceId = "00000000-0000-0000-0000-000000000000"
var now = DateTime.UtcNow;
var lastActivityThreshold = now.Subtract(TimeSpan.FromSeconds(10));
var response = client.UpdateItem(new UpdateItemRequest
{
TableName = "TheTable",
Key = new Dictionary<string, AttributeValue> { { "SequenceId", new AttributeValue { S = sequenceId } } },
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{":MessageId", new AttributeValue {S = messageId}},
{":Now", new AttributeValue {N = now.Ticks.ToString()}},
{":LastActivityThreshold", new AttributeValue {N = lastActivityThreshold.Ticks.ToString() }},
},
UpdateExpression = "REMOVE Messages[0] SET LastActivity = :Now",
ConditionExpression = "Messages[0] <> :MessageId AND (LastActivity <= :LastActivityThreshold OR attribute_not_exists(LastActivity))",
ReturnValues = ReturnValue.UPDATED_NEW
});
This is the document I'm trying to update (as seen in JSON view in the AWS Management Console):
{
"LastActivity": {
"N": "635753575712635873"
},
"Messages": {
"SS": [
"f244ed33-d678-4763-8f46-0f06514d5139",
"f668d2a5-3a4a-4564-8384-5b5a51c9bad3"
]
},
"SequenceId": {
"S": "00000000-0000-0000-0000-000000000000"
}
}
I've tried many variations of the code above, striaght down to removing all ExpressionAttributeValues
and the ConditionExpression
and just using REMOVE Messages[0]
, but it doesn't work and throws the same error.
Upvotes: 2
Views: 1749
Reputation: 491
It looks like you're trying to apply a document path to a non JSON item. There's no concept of ordering in a set, so if you want to remove the first item, you'll need to load it into memory and iterate over it. In short, you'll need to use a list in this case.
Upvotes: 1