如何从DynamoDB中的地图列表中删除(必须是原子的) [英] How to Remove from List Of Maps in DynamoDB (Must be Atomic)

查看:115
本文介绍了如何从DynamoDB中的地图列表中删除(必须是原子的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模式:

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

每个媒体项均为像这样:

Each media item will be like this:

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

样本数据:

{
    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"
        }
    ]
}

我希望能够删除从媒体列表按ID。
类似于:从产品: iPhone 6+,删除ID为 2的媒体。

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

查询后,数据应为像这样:

After the query, the Data should be like this:

{
    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"
        }
    ]
}

我应该如何表达这样的查询?我在UpdateItem上看到了一条帖子,但是找不到这种查询类型的好例子。

How should i express query like this? I saw a post on UpdateItem but i can't find a good example for this query type.

谢谢!

推荐答案

不幸的是,API没有此功能。如果您知道索引,最接近的操作就是从列表数据类型中删除一个条目。

Unfortunately, the API doesn't have this feature. The closest you can do is to delete an entry from "List" data type if you know the index.

我知道大多数时候索引可能不是可用。但是,如果没有其他解决方案,您可以看看这个替代选项。

I understand that most of the time the index mayn't be available. However, you can take a look at this alternate option if you don't have any other solution.

您还需要了解,即使DynamoDB开始支持文档数据。类型(如列表,地图和集合),则无法执行某些操作。 API中尚未添加某些功能。我相信这种情况就是其中一种。

You also need to understand that even though DynamoDB started supporting the Document data types such as List, Map and Set, you can't perform some actions. Some features are yet to be added in the API. I believe this scenario is one of them.

我已经使用删除从列表中删除了该项目。

I have used REMOVE to delete the item from list.

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));
    }
});

这仅供您参考:-

删除运算符只能在SET上使用。

The Delete operator can be used only on SET.


DELETE-从中删除元素> set

如果指定了一组值,则将从旧组的
中减去这些值。例如,如果属性值是集合[a,b,c]
,而DELETE操作指定了[a,c],则最终属性值
是[b]。指定空集是错误。

If a set of values is specified, then those values are subtracted from the old set. For example, if the attribute value was the set [a,b,c] and the DELETE action specifies [a,c], then the final attribute value is [b]. Specifying an empty set is an error.

DELETE操作仅支持集合数据类型。此外,DELETE
仅可用于顶级属性,而不能用于嵌套属性。

The DELETE action only supports set data types. In addition, DELETE can only be used on top-level attributes, not nested attributes.

这篇关于如何从DynamoDB中的地图列表中删除(必须是原子的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆