JSON array update in AWS dynamoDB

My document is as follows:

{ "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": {} } ] } } 

Let's say I need to update the member JSON array with attendeeId: 2016102973634-df . I tried many ways with the expression update and condition , but did not succeed.

Here is my attempt:

 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 resources for updating Json in the array.

After the comment by @notionquest: - I did not use JsonMarshaller. I originally added an empty field to the visitors field as follows:

 { "eventId": "20161029125458-df-d", "name": "first", "purpose": "test", "location": "yokohama", "dateArray": [], "attendees": [] } 

and then when a new visitor appears, I will add it to the participants property, for example:

 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 saw in the above snippets, first I add an empty array [] and add a new member using the code above . Now how to update a specific JSON in an array. If you say that this is not possible, what else can I try?

Should I try the following:

  • Get full JSON.
  • Manipulate JSOn and change what I want in my node.
  • And then upgrade the new JSON to dynamoDB.
  • But it consumes two dynamoDB calls that seem inefficient.

I would like to know if there is any round way?

+2
json arrays amazon-web-services amazon-dynamodb dynamo-local
source share
3 answers

I could not find the answer to the request and update the JSON array. I think this could be a profitable AWS motive to prevent these features. If you need to request a specific identifier other than the primary key, you need to make a secondary index that is cost effective. This secondary index value is additional to the din value of the amoDB table.

Since I did not want to pay extra money for the secondary index, I changed the dynamoDB scheme 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": {} } } } } 

Change members from [] to {} . This allows me to flexibly request a specific visitor and change all the JSON associated with it. Despite this, this is an extra step, I do not want to spend extra money on my hobby.

+1
source share

You can store the list index. when 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); }); 
0
source share

Example update request:

Data structure (stored in DynamoDB)

 { tenant_id: 'tenant_1', users: { user1: { _id: 'user1', email_address: ' test_email_1@gmail.com ' }, user2: { _id: 'user2', email_address: ' test_email_2@gmail.com ' } } } 

Data for updating (used in parameters)

 var user = { email_address: ' updated@gmail.com ' } 

Params

 var params = { TableName: 'tenant-Master', Key: { "tenant_id": 'tenant_1' }, UpdateExpression: "set #users.user1 = :value", ExpressionAttributeNames: { "#users": "users" }, ExpressionAttributeValues: { ":value": user, }, }; 

explanation

Switching to the map card from the map array, we can now use UpdateExpression: "set #users.user1 = :value" to update our nested object on the users map with user1 identifier.

Note: This method, as it will replace the entire map object with users.user1 . Some changes will need to be made if you want to keep previously existing data.

0
source share

All Articles