My document looks like this:
我的文件是这样的:
{
"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": {}
}
]
}
}
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.
例如,我需要用attendeeId: 2016102973634-df更新attendee JSON数组。我尝试了许多使用update和condition expression的方法,但是没有成功。
Here is my try:
这是我的尝试:
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);
});
Could not find any resources for updating an Json in a array.
无法找到任何用于更新数组中的Json的资源。
After @notionquest's comment: - Have not used any JsonMarshaller. Initially I added the empty array to attendees field like this:
@notionquest的评论:-没有使用任何JsonMarshaller。最初,我将空数组添加到参与者字段,如下所示:
{
"eventId": "20161029125458-df-d",
"name": "first",
"purpose": "test",
"location": "yokohama",
"dateArray": [],
"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);
});
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。如果你说那是不可能的,我还能尝试什么?
Should I try this :
我应该试试这个吗?
- Get the Full JSON.
- 得到完整的JSON。
- Manipulate the JSOn and change the things I want in my nodeJS.
- 操作JSOn并更改nodeJS中需要的内容。
- And then update the new JSON to dynamoDB.
- 然后将新的JSON更新到dynamoDB。
- But this consumes two calls to dynamoDB which seems to be inefficient.
- 但是这消耗了对dynamoDB的两次调用,这似乎是低效的。
Would like to know If there is any round way ?
想知道是否有圆的路?
2 个解决方案
#1
1
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.
我找不到任何查询和更新json数组的答案。我认为这可能是AWS不允许这些功能的盈利动机。如果需要查询除主键之外的特定ID,则需要创建一个具有成本效益的辅助索引。这个次要的索引成本是dyn amoDB表成本的附加部分。
Since, I did not want to pay extra bucks on secondary index, I changed my dynamoDB schema to the following:
因为,我不想在二级索引上多花些钱,所以我将我的dynamoDB模式改为如下:
{
"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": {}
}
}
}
}
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.
改变参会者从[]到{}。这允许我查询特定的attendeeId并更改与之关联的整个JSON。尽管如此,这是一个多余的步骤,我不想花额外的钱在我的兴趣项目上。
#2
0
you can store the index of list. while updating the list we can use them. For example ,
可以存储列表的索引。在更新列表时,我们可以使用它们。例如,
{
"data": {
"eventId": "20161029125458-df-d",
"name": "first",
"purpose": "test",
"location": "yokohama",
"dateArray": [],
"attendees": [
{
"index":0,
"attendeeId": "2016102973634-df",
"attendeeName": "lakshman",
"personalizedDateSelection": {}
},
{
"index":1,
"attendeeId": "2016102973634-tyyu",
"attendeeName": "diwaakar",
"personalizedDateSelection": {}
}
]
}
}
const params = {
TableName: "event",
Key: {
"eventId": eventId
},
UpdateExpression: "SET attendees[attendee.index].attendeeName = :value",
ExpressionAttributeValues: {
":value" : {"S":"karthik"}
},
ReturnValues: "ALL_NEW"
};
dynamo.update(params, (err, data) => {
if (err) {
return reject(err);
}
console.log(data.Attributes);
});
#1
1
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.
我找不到任何查询和更新json数组的答案。我认为这可能是AWS不允许这些功能的盈利动机。如果需要查询除主键之外的特定ID,则需要创建一个具有成本效益的辅助索引。这个次要的索引成本是dyn amoDB表成本的附加部分。
Since, I did not want to pay extra bucks on secondary index, I changed my dynamoDB schema to the following:
因为,我不想在二级索引上多花些钱,所以我将我的dynamoDB模式改为如下:
{
"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": {}
}
}
}
}
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.
改变参会者从[]到{}。这允许我查询特定的attendeeId并更改与之关联的整个JSON。尽管如此,这是一个多余的步骤,我不想花额外的钱在我的兴趣项目上。
#2
0
you can store the index of list. while updating the list we can use them. For example ,
可以存储列表的索引。在更新列表时,我们可以使用它们。例如,
{
"data": {
"eventId": "20161029125458-df-d",
"name": "first",
"purpose": "test",
"location": "yokohama",
"dateArray": [],
"attendees": [
{
"index":0,
"attendeeId": "2016102973634-df",
"attendeeName": "lakshman",
"personalizedDateSelection": {}
},
{
"index":1,
"attendeeId": "2016102973634-tyyu",
"attendeeName": "diwaakar",
"personalizedDateSelection": {}
}
]
}
}
const params = {
TableName: "event",
Key: {
"eventId": eventId
},
UpdateExpression: "SET attendees[attendee.index].attendeeName = :value",
ExpressionAttributeValues: {
":value" : {"S":"karthik"}
},
ReturnValues: "ALL_NEW"
};
dynamo.update(params, (err, data) => {
if (err) {
return reject(err);
}
console.log(data.Attributes);
});