简体   繁体   中英

How to update part of an object in an item in Dynamo using aws-sdk?

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"
            }
        ]
    }

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
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM