数据库中项目的结构如下所示:
{
"cars": {
"x": [
{
"time": 1485700907669,
"value": 23
}
]
},
"date": 1483214400000,
"id":"1"
}
我必须向汽车添加一个列表类型的新项目“z”,例如
{
"cars": {
"x": [
{
"time": 1485700907669,
"value": 23
}
],
"z": [
{
"time": 1485700907669,
"value": 23
}
]
},
"date": 1483214400000,
"id": "1"
}
如果我想实现这样的目标,Node.js 中的更新表达式会是什么样子?
到目前为止,这就是我想出的:
设置#car.#model= list_append(if_not_exists(#car.#model, :empty_list), :value)
但是,如果该项目在创建时不存在,则会抛出错误。知道如何做到这一点吗?
这是我正在使用的更新参数,仍然不起作用
var params = {
TableName:table,
Key:{
"id": id,
"date": time.getTime()
},
ReturnValues: 'ALL_NEW',
UpdateExpression: 'SET #car.#model = if_not_exists(#car.#model,
:empty_list)',
ExpressionAttributeNames: {
'#car': 'cars',
'#model':"z"
},
ExpressionAttributeValues: {
':empty_list': [],
}
};
最佳答案
解决方案是分两步更新操作,首先为父级创建一个空映射,因为它首先不存在。 所以,就我而言
SET #car= :empty_map
其中:empty_map = {}
执行此操作后运行其他更新表达式
SET #car.#model = list_append(if_not_exists(#car.#model, :empty_list), :value)
其中 :empty_list=[] 和 :value= { “时间”:1485700907669, “值”:23 }
关于json - 使用 Nodejs 将 DynamoDB 中的 map 结构插入到现有 map 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41922705/