AWS DynamoDB 中的递增数字 属性

Increment Number Property in AWS DynamoDB

如何在 AWS Dynamodb 中递增数字?

指南在保存项目时说,只需重新保存即可: http://docs.aws.amazon.com/mobile/sdkforios/developerguide/dynamodb_om.html

但是我正在尝试使用一个计数器,许多用户可能同时更新。

其他文档告诉我使用和 UpdateItem 操作,但我找不到这样做的好例子。

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html

但是,我找不到实现表达式的方法。将来我将向数组和映射添加值。这会一样吗?我的代码在 Obj C

目前我的代码如下所示:

AWSDynamoDBUpdateItemInput *updateItemInput = [AWSDynamoDBUpdateItemInput new];
        updateItemInput.tableName = @"TableName";
        updateItemInput.key = @{
                                UniqueItemKey:@"KeyValue"
                                };
        updateItemInput.updateExpression = @"SET counter = counter + :val";
        updateItemInput.expressionAttributeValues =@{
                                                     @":val":@1
                                                     };

您似乎遗漏了最后一段实际发出更新项目请求的代码:

AWSDynamoDB *dynamoDB = [AWSDynamoDB defaultDynamoDB];

[[dynamoDB updateItem:updateItemInput]
continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"The request failed. Error: [%@]", task.error);
    }
    if (task.exception) {
        NSLog(@"The request failed. Exception: [%@]", task.exception);
    }
    if (task.result) {
        //Do something with result.
    }
    return nil;
}];

在 DynamoDB 中,如果您想增加任何 propertie/field 的值,您可以使用带有操作选项 ADD 的 UpdateItemRequest。我在 android 中使用了这个方法来更新字段的现有值。让我分享代码片段。您可以使用任何操作,例如添加、删除、放置等。

.....
AttributeValue viewcount = new AttributeValue().withS("100");
AttributeValueUpdate attributeValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.ADD).withValue(viewcount);
updateItems.put(UploadVideoData.FIELD_VIEW_COUNT, attributeValueUpdate);

UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(UploadVideoData.TABLE_NAME)
                                .withKey(primaryKey).withAttributeUpdates(updateItems);

UpdateItemResult updateItemResult = amazonDynamoDBClient.updateItem(updateItemRequest);
....

您可以看到上面的代码会将 100 个计数添加到该字段的现有值中。

此代码适用于 android,但技术将保持不变。

谢谢。