0

Following is sample record of an item from my dynamo table. Here, schedule object has two properties and I just to update hours field . Is it even possible?

WHAT I KNOW IS:

  • I can update name property.
  • I can update entire schedule object.
  • Here, schedule is tricky object where I may have more attribute as well. I am 100% sure that hours will be there with a given structure, so I just want to update it if possible.

CODE FOR FIRST TWO BULLET POINTS:

var params = {
    TableName: tableName,
    Key: {
        "primaryKey": primaryKey,
        "sortKey": SOME-KEY,
    },
    UpdateExpression: 'set #name = :name',
    ExpressionAttributeNames: {
        "#name": "name",
    },
    ExpressionAttributeValues: {
        ':name': orderModel.name,
    }
};

return DynamoDb.update(params, function (err, data) {
    if (err) {
        console.log(err, err.stack); // an error occurred
    } else {
        // console.log("Update:", data); // successful response
    }
});
 

SAMPLE DYNAMO RECORD:

   {
        "name": "test",
        "schedule": [
            {
                "hours": {
                    "FRI": [
                        {
                            "endTime": "21:11:16.508",
                            "startTime": "16:38:18.713"
                        }
                    ],
                    "MON": [
                        {
                            "endTime": "18:45:00.000",
                            "startTime": "15:20:18.947"
                        }
                    ],
                    "SAT": [
                        {
                            "endTime": "10:26:26.512",
                            "startTime": "09:00:28.532"
                        }
                    ],
                    "SUN": [
                        {
                            "endTime": "23:44:17.370",
                            "startTime": "03:16:34.433"
                        }
                    ],
                    "THU": [
                        {
                            "endTime": "19:44:26.015",
                            "startTime": "10:28:06.529"
                        }
                    ],
                    "TUE": [
                        {
                            "endTime": "16:04:54.295",
                            "startTime": "03:56:54.118"
                        }
                    ],
                    "WED": [
                        {
                            "endTime": "21:01:29.798",
                            "startTime": "03:58:49.007"
                        }
                    ]
                },
                "name": "8s9un1g86J"
            }
        ]
    }
2
  • 1
    stackoverflow.com/questions/45784042/… Commented Nov 20, 2020 at 1:19
  • @hoangdv this is excellent link. I am able to get my desired results. Thank you. Commented Nov 20, 2020 at 20:44

1 Answer 1

0

Following will work to update inside an array of object.

var params = {
    TableName: tableName,
    Key: {
        "primaryKey": primaryKey,
        "sortKey": SOME-KEY,
    },
    ReturnValues: 'ALL_NEW',
    UpdateExpression: 'set schedule[' + `0` + '].hours = :UPDATED-DATA',
    ExpressionAttributeValues: { ':UPDATED-DATA': YOUR-UPDATED-DATA }
};

return DynamoDb.update(params, function (err, data) {
    if (err) {
        console.log(err, err.stack); // an error occurred
    } else {
        // console.log("Update:", data); // successful response
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.