gpt4 book ai didi

amazon-dynamodb - 如何从 DynamoDB 中的 map 列表中删除(必须是原子的)

转载 作者:行者123 更新时间:2023-12-04 07:01:39 25 4
gpt4 key购买 nike

我有这个架构:

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

每个媒体项目将是这样的:
[
{
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’的媒体”

查询后,数据应该是这样的:
{
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 上看到了一个帖子,但我找不到这个查询类型的好例子。

谢谢!

最佳答案

不幸的是,API 没有这个功能。如果您知道索引,您可以做的最接近的事情是从“列表”数据类型中删除条目。

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

您还需要了解,即使 DynamoDB 开始支持 List、Map 和 Set 等 Document 数据类型,您也无法执行某些操作。 API 中尚未添加某些功能。我相信这个场景就是其中之一。

我用过 删除 从列表中删除项目。

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 - Deletes an element from a set.

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.

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

关于amazon-dynamodb - 如何从 DynamoDB 中的 map 列表中删除(必须是原子的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39014352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com