updating a JSON array in AWS dynamoDB

后端 未结 3 1191
你的背包
你的背包 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

    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.

提交回复
热议问题