Add/remove one element from a string set in DynamoDBItem

大憨熊 提交于 2019-12-25 08:49:47

问题


I am using DynamoDB for a social app based on events, like-unlike attending-not_attending.

I am interested in finding out how can add or remove an item from a StringSet attribute in DynamoDB Item.

Here is the objects structure

  "userId": {
    "S": "69"
  },
  "fName": {
    "S": "mfName"
  },
  "lName": {
    "S": "mlName"
  },
  "picture": {
    "S": "https://s3-us-west-2.amazonaws.com/users/mfName_1000049788.jpg"
  },
  "events": {
    "M": {
      "1502199358": {
        "M": {
          "created": {
            "S": "1502199358.443022"
          },
          "eventId": {
            "S": "-1"
          },
          "galleryId": {
            "S": "1502199358"
          },
          "image": {
            "S": "https://s3.amazonaws.com/events/public/clyp_1502199358.jpg"
          },
          "likes": {
            "SS": [
              "12",
              "321",
              "69"
            ]
          },
          "modified": {
            "S": "1"
          },
          "video": {
            "S": "n_p"
          },
          "videothumb": {
            "S": "n_p"
          }
        }
      }
    }
  }

and I would like to add or remove one entry in "likes" StringSet "likes": { "SS": [ "12", "321", "69" ] }, accordingly a user like-unlike the event. how can I do this?


回答1:


I did not get able to update/edit item on third or 4th child level.

Here you can use this method to add/delete item from a string set on second child level.

AWSDynamoDB *dynamoDB = [AWSDynamoDB defaultDynamoDB];

AWSDynamoDBAttributeValue * itemToUpdate = [AWSDynamoDBAttributeValue new];
itemToUpdate.S = self.userID;

AWSDynamoDBUpdateItemInput *updateItemInput = [AWSDynamoDBUpdateItemInput new];
updateItemInput.tableName = @"MyTable";
updateItemInput.key= @{
                       @"userId" : itemToUpdate
                       };

NSArray * likesArray = [NSArray arrayWithObjects:@"12", nil];
AWSDynamoDBAttributeValue * awsDynamoDBAttributeValue = [AWSDynamoDBAttributeValue new];
awsDynamoDBAttributeValue.SS = likesArray;

AWSDynamoDBAttributeValueUpdate * valueUpdate = [AWSDynamoDBAttributeValueUpdate new];
valueUpdate.value = awsDynamoDBAttributeValue;
valueUpdate.action = AWSDynamoDBAttributeActionDelete;

updateItemInput.attributeUpdates =@{
                                    @"likes":valueUpdate
                                };

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

-> for add an item valueUpdate.action = AWSDynamoDBAttributeActionAdd;

-> for delete an item valueUpdate.action = AWSDynamoDBAttributeActionDelete;

Hope it would help someone.



来源:https://stackoverflow.com/questions/45629206/add-remove-one-element-from-a-string-set-in-dynamodbitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!