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

查看:10
本文介绍了如何从 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 开始支持 List、Map 和 Set 等 Document 数据类型,您也无法执行某些操作.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.

我已使用 REMOVE 从列表中删除项目.

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

仅供参考:-

Delete 运算符只能用于 SET.

The Delete operator can be used only on SET.

DELETE - 从 set 中删除一个元素.

DELETE - Deletes an element from a 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 操作仅支持设置数据类型.此外,删除只能用于顶级属性,不能用于嵌套属性.

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天全站免登陆