DynamoDB Update Deep Nested Attributes

0

I have the following data structure with multi-level Map columns/items:

{
"pk": {
    "S": "cf633075-e808-4e33-9fc1-08301b54384b"
},
"active": {
    "N": "1"
},
"delta": {
    "N": "-0.9371997798455771"
},
"dte": {
    "N": "0.01893752246685426"
},
"hedge_positions": {
  "M": {
    "151e9c9f-aa5d-4f5d-9826-74011da03137": {
      "M": {
        "active": {
          "N": "1"
        },
        ...
      }
    },
    "1bed59c9-6f73-48ae-8b65-62fd1f10ddad": {
      "M": {
        "active": {
          "N": "1"
        },
        ....
      }
    },
    "24cf6015-780e-408d-b0e1-935ae2c7199d": {
      "M": {
        "active": {
          "N": "1" //Update this to 0
        },
        ...
      }
    }
  }
}

I now wish to update the value of "active" for one of the "hedge_positions" to 0. The hedge_positions is a Map, containing unique Maps. In Javascript, I have built the following update, but I am recieving the error "The provided expression refers to an attribute that does not exist in the item".

const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB({region: process.env.AWS_REGION});

var hID = "24cf6015-780e-408d-b0e1-935ae2c7199d";

var params = {
            TableName: Positions,
            Key: {
                pk: {"S": positionId }
            },
            UpdateExpression: "SET #hp.#id.#a = :a",
            ExpressionAttributeValues: {":a": {"N": "0"}},
            ExpressionAttributeNames: {"#hp": "hedge_positions", "#id": hID, "#a": "active"}
};

await dynamoDb.updateItem(params).promise();

I believe this to be because I am not specifying that the "GUID" is itself a Map. I have tried to set the UpdateExpression to "SET #hp.#id.M.#a = :a" but to no avail. What have I set incorrectly in the UpdateExpression?

gefragt vor 2 Jahren5112 Aufrufe
1 Antwort
1

I tested this in my own account and it works with no issue, returning the NEW_IMAGE in the response to ensure the update was successful:

const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB({region: 'eu-west-1'});

var hID = "24cf6015-780e-408d-b0e1-935ae2c7199d";

var params = {
    TableName: "cust1",
    Key: {
        pk: { "S": "123" },
        sk: { "S": "abc" }
    },
    UpdateExpression: "SET #hp.#id.#a = :a",
    ExpressionAttributeValues: { ":a": { "N": "0" } },
    ExpressionAttributeNames: { "#hp": "hedge_positions", "#id": hID, "#a": "active" }, 
    ReturnValues: "ALL_NEW"
};

dynamoDb.updateItem(params)
.promise()
.then(res=>console.log(JSON.stringify(res)))
.catch(err=>console.log(err));

Item before:

{
 "pk": "123",
 "sk": "abc",
 "active": 1,
 "delta": -0.9371997798455771,
 "dte": 0.01893752246685426,
 "hedge_positions": {
  "151e9c9f-aa5d-4f5d-9826-74011da03137": {
   "active": 1
  },
  "1bed59c9-6f73-48ae-8b65-62fd1f10ddad": {
   "active": 1
  },
  "24cf6015-780e-408d-b0e1-935ae2c7199d": {
   "active": 1
  }
 }
}

Item After:

{
 "pk": "123",
 "sk": "abc",
 "active": 1,
 "delta": -0.9371997798455771,
 "dte": 0.01893752246685426,
 "hedge_positions": {
  "151e9c9f-aa5d-4f5d-9826-74011da03137": {
   "active": 1
  },
  "1bed59c9-6f73-48ae-8b65-62fd1f10ddad": {
   "active": 1
  },
  "24cf6015-780e-408d-b0e1-935ae2c7199d": {
   "active": 0
  }
 }
}
profile pictureAWS
EXPERTE
beantwortet vor 2 Jahren
profile pictureAWS
EXPERTE
Chris_G
überprüft vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen