updating a JSON array in AWS dynamoDB

后端 未结 3 1188
你的背包
你的背包 2020-12-11 20:19

My document looks like this:

{
  \"data\": {
      \"eventId\": \"20161029125458-df-d\",
      \"name\": \"first\",
      \"purpose\": \"test\",
      \"loca         


        
相关标签:
3条回答
  • 2020-12-11 20:53

    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);
    });
    
    0 讨论(0)
  • 2020-12-11 20:53

    An example of an update query:

    Data structure (saved 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 update (used in the params)

    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

    By switching to a map of maps from an array of maps we can now use UpdateExpression: "set #users.user1 = :value" to update our nested object at the map of users with the id of user1.

    NOTE: This method as is will REPLACE the entire map object at users.user1. Some changes will need to be made if you want to keep pre-existing data.

    0 讨论(0)
  • 2020-12-11 21:05

    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.

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

    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.

    0 讨论(0)
提交回复
热议问题