Home > Net >  How to update map property in dynamodb
How to update map property in dynamodb

Time:12-22

I have a map in dynamo with the following structure:

 participants: {
      M: {
        "6ecd8c99-4036-403d-bf84-cf8400f67836": {
          M: {
            participantUid: {
              S: "6ecd8c99-4036-403d-bf84-cf8400f67836",
            },
            userSummary: {
              M: {
                userRef: {
                  S: "refGoesHere",
                },
              },
            },
            isRead: {
              BOOL: false,
            },
          },
        },
      },
    },

I'm trying to update the isRead property with the following command:

const updateReadStatusCommand = new UpdateCommand({
      TableName: config.database,
      Key: {
        PK: `TASK#${taskUid}#CLIENT#${client}`,
        SK: `THREAD#${uid}`,
      },
      UpdateExpression: `SET participants.#participantUid.isRead = :isRead`,
      ExpressionAttributeNames: {
        "#participantUid": userUid,
      },
      ExpressionAttributeValues: {
        ":isRead": updateBody.isRead,
      },
      ReturnValues: "ALL_NEW",
    });

So far I keep running into the following error: The document path provided in the update expression is invalid for update.

I'm struggling to see the issue here, as it won't recognise the property within the map. Can anyone point me in the right direction?

Edit:

I've also scanned the table to ensure that the data is in the correct format and it looks like this:

"participants": {
        "6ecd8c99-4036-403d-bf84-cf8400f67836": {
          "isRead": false,
          "participantUid": "6ecd8c99-4036-403d-bf84-cf8400f67836",
          "userSummary": {
            "userRef": "refGoesHere"
          }
        }
      },

CodePudding user response:

I dont see much wrong with this, the only thing I can think of it trying to change -

UpdateExpression: `SET participants.#participantUid.isRead = :isRead`,
ExpressionAttributeNames: {
   "#participantUid": userUid,
},
...

to

UpdateExpression: `SET participants.#participantUid.#isRead = :isRead`,
ExpressionAttributeNames: {
   "#participantUid": userUid,
   "#isRead": "isRead"
},
...

in case .isRead is being treated as a string, rather than part of the path, as I noticed in the docs, like the example below, the use of #3str being used as #pr.#3star rather than using #pr.ThreeStar directly -

aws dynamodb update-item \
    --table-name ProductCatalog \
    --key '{"Id":{"N":"789"}}' \
    --update-expression "SET #pr.#5star[1] = :r5, #pr.#3star = :r3" \
    --expression-attribute-names file://names.json \
    --expression-attribute-values file://values.json \
    --return-values ALL_NEW

(names.json)

{
    "#pr": "ProductReviews",
    "#5star": "FiveStar",
    "#3star": "ThreeStar"
}

CodePudding user response:

Solved this due to an incorrect SK which meant that the document I wanted to update couldn't be found.

The query itself was correct. Leaving this here for anyone in a similar position, check you're actually trying to update the right thing!

  • Related