如何在 dynamodb 中添加空映射或数据映射? [英] how to add an empty or data map in dynamodb?
问题描述
我有这样的事情:
{
Records:{}
}
我只想在这样的记录中添加数据:
and I just want to add data inside records like this:
{
Id: 123,
Records:{
10001: { event : "item1" }
}
}
我的第一次尝试:
var params = {
TableName : "Records",
Key : { Id : 123 },
UpdateExpression: 'set Record.#k1.event = :v1',
ExpressionAttributeNames: { '#k1' : 10001},
ExpressionAttributeValues: { ':v1' : 'item1' }
};
因为 Record.10001 不存在,所以它给我错误:
Because Record.10001 doesn't exist, it give me error:
The document path provided in the update expression is invalid for update
"ADD" 仅支持 NUMBER 和 SET 类型,因此不适合我的情况.
"ADD" only support NUMBER and SET type so its not suitable in my case.
我的第二次尝试(第一次尝试创建空地图):
My 2nd attempt (trying to create a empty map 1st):
var params = {
TableName : "Records",
Key : { Id : 123 },
UpdateExpression: 'set Record.#k1 = {}',
ExpressionAttributeNames: { '#k1' : 10001},
};
它失败了,因为我的语法可能不正确,而且我找不到如何为属性创建一个空映射.
It fails as my syntax is probably incorrect and I cannot find how to create a empty map to an attribute.
所以我的问题是如何将结构化对象作为地图或空地图添加到现有项目,如上面的示例?
So my question is how do I add structured object as a map or empty map to an existing item like the example above?
我可以轻松地从 amazon DynamoDB 数据管理页面修改数据,所以至少我知道有一种方法可以做到这一点.我只需要知道怎么做.
I can easily modifying the data from the amazon DynamoDB data management page so at least I know there exist a way to do it. I just need to know how.
谢谢
推荐答案
我以某种方式成功地做到了:
I somehow successfully done it this way:
var params = {
TableName: "Records",
Key: { Id: 123 },
UpdateExpression: 'set Records.#k1 = :v1',
ExpressionAttributeNames: {'#k1': '10001',},
ExpressionAttributeValues: {':v1': { event: 'Yay~' } }
};
你可以选择使用
UpdateExpression: 'set Records.#k1 = if_not_exists( Records.#k1, :v1)',
避免覆盖现有记录.
这篇关于如何在 dynamodb 中添加空映射或数据映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!