How to remove from the list of maps in DynamoDB (must be Atomic)

I have this circuit:

{
   product: S // Primary Key, // my Hash
   media: L // List of Maps
}

Each media item will look like this:

[
   { 
      id: S, // for example: id: uuid()
      type: S, // for example: "image"
      url: S // for example: id: "http://domain.com/image.jpg"
   }
]

Sample data:

{
    product: "IPhone 6+",
    media: [
        {
            id: "1",
            type: "image",
            url: "http://www.apple.com/iphone-6-plus/a.jpg"
        },
        {
            id: "2",
            type: "image",
            url: "http://www.apple.com/iphone-6-plus/b.jpg"
        },
        {
            id: "3",
            type: "video",
            url: "http://www.apple.com/iphone-6-plus/overview.mp4"
        }
    ]
}

I want to remove from the media list by id. Something like: "From the product:" IPhone 6+ ", remove the media with the id:" 2 ""

After the request, the data should be as follows:

{
    product: "IPhone 6+",
    media: [
        {
            id: "1",
            type: "image",
            url: "http://www.apple.com/iphone-6-plus/a.jpg"
        },
        {
            id: "3",
            type: "video",
            url: "http://www.apple.com/iphone-6-plus/overview.mp4"
        }
    ]
}

How can I express such a request? I saw a post in UpdateItem, but I cannot find a good example for this type of request.

Thank!

0
source share
1 answer

Unfortunately, the API does not have this feature. The closest thing you can do is remove the entry from the List data type if you know the index.

, . , .

, DynamoDB Document, List, Map Set, . API. , .

REMOVE, .

var params = {
        TableName : "Product",
        Key : {
            "product" : "IPhone 6+"
        },
        UpdateExpression : "REMOVE media[0]",       
        ReturnValues : "UPDATED_NEW"
    };

console.log("Updating the item...");
docClient.update(params, function(err, data) {
    if (err) {
        console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("UpdateItem succeeded:", JSON.stringify(data));
    }
});

: -

Delete SET.

DELETE - set.

, . , [a, b, c] DELETE [a, c], ]. .

DELETE . , DELETE , .

+2

All Articles