更新 AWS dynamoDB 中的 JSON 数组 [英] updating a JSON array in AWS dynamoDB
问题描述
我的文档如下所示:
{
"data": {
"eventId": "20161029125458-df-d",
"name": "first",
"purpose": "test",
"location": "yokohama",
"dateArray": [],
"attendees": [
{
"attendeeId": "2016102973634-df",
"attendeeName": "lakshman",
"personalizedDateSelection": {}
},
{
"attendeeId": "2016102973634-tyyu",
"attendeeName": "diwaakar",
"personalizedDateSelection": {}
}
]
}
}
比如说,我需要用 attendeeId: 2016102973634-df 更新参加者 JSON 数组.我尝试了很多方法使用更新和条件表达式,但没有成功.
Say, I need to update the attendee JSON array with attendeeId: 2016102973634-df. I tried many ways ways using update and condition expression, but no success.
这是我的尝试:
const params = {
TableName: "event",
Key: {
"eventId": eventId
},
UpdateExpression: "SET attendees[???] = ",
ConditionExpression: attendees.attendeeId = "2016102973634-df",
ExpressionAttributeValues: {
":attendee" : attendeeList
},
ReturnValues: "ALL_NEW"
};
dynamo.update(params, (err, data) => {
if (err) {
return reject(err);
}
console.log(data.Attributes);
});
找不到用于更新数组中的 Json 的任何资源.
Could not find any resources for updating an Json in a array.
@notionquest 发表评论后:- 没有使用任何 JsonMarshaller.最初我将空数组添加到参加者字段,如下所示:
After @notionquest's comment: - Have not used any JsonMarshaller. Initially I added the empty array to attendees field like this:
{
"eventId": "20161029125458-df-d",
"name": "first",
"purpose": "test",
"location": "yokohama",
"dateArray": [],
"attendees": []
}
然后当新的 attendee 出现时,我将其添加到 attendees 属性中,如下所示:
and then When a new attendee comes I add it to the attendees property like this:
const attendee = {
"attendeeName": "user1",
"personalizedDateSelection": {"today": "free"}
}
const attendeeList = [attendee];
const eventId = "20161029125458-df-d";
const params = {
TableName: "event",
Key: {
"eventId": eventId
},
UpdateExpression: "SET attendees = list_append(attendees, :attendee)",
ExpressionAttributeValues: {
":attendee" : attendeeList
},
ReturnValues: "ALL_NEW"
};
dynamo.update(params, (err, data) => {
if (err) {
return reject(err);
}
console.log("in update dynamo");
console.log(data.Attributes);
});
正如您在上面的代码片段中看到的,最初我添加了空的 [] 数组,并使用上面的代码添加了一个新的与会者.现在,如何更新数组中的特定 JSON.如果你说这是不可能的,我还能尝试什么?
As you have seen in the above snippets, initially I add empty [] array and add a new attendee using the above code. Now, How do I update a specific JSON in an array. If you say that is not possible, what else can I try?
我应该试试这个:
- 获取完整的 JSON.
- 操作 JSOn 并在我的 nodeJS 中更改我想要的东西.
- 然后将新的 JSON 更新为 dynamoDB.
- 但这会消耗对 dynamoDB 的两次调用,这似乎是低效的.
想知道是否有任何圆形方式?
Would like to know If there is any round way ?
推荐答案
我找不到任何答案来查询和更新 JSON 数组.我认为这可能是 AWS 不允许这些功能的盈利动机.如果您需要查询除主键之外的特定ID,则需要制作具有成本效益的二级索引.此二级索引成本是 dyn 的额外成本amoDB 表开销.
I could not find any answer to query and update the JSON-array. I think this may be AWS profitable motive to not allow those features. If you need to query on a particular ID other than primary key, you need to make a secondary index which is cost effective. This secondary index cost is additional to the dyn amoDB table cost.
由于我不想为二级索引支付额外费用,因此我将 dynamoDB 架构更改为以下内容:
Since, I did not want to pay extra bucks on secondary index, I changed my dynamoDB schema to the following:
{
"data": {
"eventId": "20161029125458-df-d",
"name": "first",
"purpose": "test",
"location": "yokohama",
"dateArray": [],
"attendees": {
"2016102973634-df": {
"attendeeId": "2016102973634-df",
"attendeeName": "lakshman",
"personalizedDateSelection": {}
},
"2016102973777-df": {
"attendeeId": "2016102973777-df",
"attendeeName": "ffff",
"personalizedDateSelection": {}
}
}
}
}
将与会者从 [] 更改为 {}.这让我可以灵活地查询特定的 attendeeId 并更改与之关联的整个 JSON.尽管这是一个多余的步骤,但我不想在我的爱好项目上花费额外的钱.
Changing attendees from [] to {}. This allows me the flexibility to query particular attendeeId and change the entire JSON associated with that. Even though, this is a redundant step, I do not want to spend extra bucks on my hobby project.
这篇关于更新 AWS dynamoDB 中的 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!