Updating a Set in Dynamo db using Node Js

♀尐吖头ヾ 提交于 2019-12-22 12:27:31

问题


I am trying to do one of the most simple operations "Update" in a dynamo db list.

Table Schema -

businessId : String, customers: StringSet,  itemCode : NumberSet

I have an entry inserted via put -

bussinessId = "sampleBusiness", cuatomers 0: "cust1", itemCode 0: 4554

I want to add more items using update and here is what I have tried -

 var updateRequest = {  
    'TableName' : tableName,
    'Key' : {
        'businessId' : {
            "S" : businessId
        }
    },
    'UpdateExpression' : "SET itemCode[2] =:attrValue",    
    'ExpressionAttributeValues' : {
        ':attrValue' : {
            "N" : "564564"
        }
    }
};

This gives me error -

Document Path provided in document is invalid

I wanted to append new entries so tried this as well -

var sm = [];
sm[0] = "56465";

//Add business to 
var updateRequest = {  
    'TableName' : tableName,
    'Key' : {
        'businessId' : {
            "S" : businessId
        }
    },
    'UpdateExpression' : 'SET #attrName = list_append(#attrName, :attrValue)',
    'ExpressionAttributeNames' : {
        '#attrName' : 'itemCode'
    },
    'ExpressionAttributeValues' : {
        ':attrValue' : {
            "NS" : sm
        }
    }
  };

This gives:

ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: NS

Also attempted this -

 ':attrValue' : {
                "N" : "4564"
            }

But same error.

As per the example provided in http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html , it adds a new element to the FiveStar review list. The expression attribute name #pr is ProductReviews; the attribute value :r is a one-element list. If the list previously had two elements, [0] and [1], then the new element will be [2].

SET #pr.FiveStar = list_append(#pr.FiveStar, :r) 

which Says :r is one element list

I am missing some thing here. Request if any one can help. Struck on this for long time. I just want to append elements in set in dynamo db using nodeJS.


回答1:


It looks like this:

'ExpressionAttributeValues' : {
    ':attrValue' : {
        "NS" : sm
    }
}

Should be this:

'ExpressionAttributeValues' : {
    ':attrValue' : {
        "S" : sm
    }
}

Or you need to cast this value sm[0] = "56465"; to a Number Number("56465") and change the :attrValue data type "S" to "N". Depends on how you have your table configured.

It's possible too that you should assign :attrValue to be "S" : sm[0] because right now you are passing an "S" a whole array.




回答2:


I got a proper solution

var item = {"endTime": "7pm", "imageName": "7abcd", "startTime": "7pm"};

dynamo.updateItem({
    TableName:'TableName',
    Key:{"BucketName":"abcdefg" }, 
    UpdateExpression : "SET #attrName = list_append(#attrName, :attrValue)",
    ExpressionAttributeNames : {
        "#attrName" : "ImageLists"
    },
    ExpressionAttributeValues : {
        ':attrValue' : [item]
    }
},function(err, data) {
    if (err)
        console.log(err);
    else
        console.log(data)
});


来源:https://stackoverflow.com/questions/33708603/updating-a-set-in-dynamo-db-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!