簡體   English   中英

更新 Dynamodb 中的項目?

[英]Update an item in Dynamodb?

我是這項技術的新手。 所以請忍受我下面的解釋。

我的用例:

我正在根據主鍵(ID)更新 Dynamodb 中的項目。 基本上我的記錄有 4 列( ID, description, name , price )。

這里的問題是假設當我有一個鍵為 123 的項目時

ID description name price

123 Model      nokia  10

當我使用上面的 lamda 函數更新所有列時,它工作正常。 但是當我嘗試僅更新description列時,它會拋出 'ExpressionAttributeValues can't be empty` 的錯誤。 所以為此我做了這樣的事情

ExpressionAttributeValues:{
     ":d":event.description ? event.description : null ,
     ":n":event.name ? event.name : null,
     ":p":event.price ? event.price : null
     }

但問題是它會更新我給出的唯一值。 就像如果我只對modal1給出了description ,那么它會改變它並給出這樣的結果

ID description name price

123 Model1  null  null

這是正確的,因為上面的代碼片段說要這樣做。

我想實現什么?

我怎樣才能改變這種行為來實現這樣的目標

ID description name price

123 Model1     nokia  10
  1. 有沒有辦法只更新傳遞的參數並將其他參數保持為以前的值?

任何幫助表示贊賞

更新代碼

var AWS = require("aws-sdk");
AWS.config.update({
    region: "regionname",
    endpoint: "endpointurl"
});
var docClient = new AWS.DynamoDB.DocumentClient()

var tablename = "Example"; 


var UpdateExpression = "'set";
var ExpressionAttributeValues= {};
var ExpressionAttributeNames= {};
var ID = "xx";
var description = "Iphone";
var name = "xx";
var price = 50;
 var params = { 
    TableName: tablename, 
    Key:{ 
    "ID": ID 
    }
    };
docClient.update(params,onUpdate);
 function onUpdate(err) {
    if (err) {
        console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Update succeeded.");
    }
    if(description) { 
        UpdateExpression = UpdateExpression + " description = :d,",
        ExpressionAttributeValues [":d"] =  description ,
        console.log("first...")

        }
        if(name){ 
        UpdateExpression = UpdateExpression + " #na = :n,",
        ExpressionAttributeNames['#na'] = 'name' ,
        ExpressionAttributeValues[":n"] = name ,
        console.log("second...")
        }
        if(price){ 
        UpdateExpression = UpdateExpression + " price = :p',",
        ExpressionAttributeValues[":p"] = price ,
        console.log("third...")
        }        
        console.log(UpdateExpression)
        console.log(ExpressionAttributeNames)
        console.log(ExpressionAttributeValues)
        return (UpdateExpression,ExpressionAttributeNames,ExpressionAttributeValues)
    }

當我運行代碼時,我得到了這樣的輸出:

D:\\Dynamo>node FullUpdate.js

Update succeeded.
first...
second...
third...
'set description = :d, #na = :n, price = :p',
{ '#na': 'name' }
{ ':d': 'Iphone', ':n': 'xx', ':p': 50 }

你必須根據你擁有的變量來構造你的查詢,所以你必須做這樣的事情

UpdateExpression = "set";
ExpressionAttributeValues: {};
ExpressionAttributeNames: {};

if(event.description){
 UpdateExpression = UpdateExpression + "description = :d";
 ExpressionAttributeValues[":d"] = event.description
}
if(event.name){
 UpdateExpression = UpdateExpression + "#na = :n";
 ExpressionAttributeNames['#na'] = 'name'
 ExpressionAttributeValues[":d"] = event.name
}
if(event.price){
 UpdateExpression = UpdateExpression + "price = :p";
 ExpressionAttributeValues[":d"] = event.pice
}

小心逗號,並記錄您的查詢以確保它符合您的期望。 您可以檢查我為此目的使用的功能,

update(_id, store_id, opts?) {

    var Key = {
      _id: _id,
      store_id: store_id
    };

    if (Object.keys(opts).length > 0) {
      let UpdateExpression =
        "SET #date = :date, #author = :author";

      let ExpressionAttributeNames = {
        "#date": "date",
        "#author": "author"
      }

      let ExpressionAttributeValues = {
        ":date": new Date().getTime(),
        ":author": localStorage.getItem('user_id')
      }

      if ((opts && opts.title)) {
        UpdateExpression = UpdateExpression + "#title = :title,";
        ExpressionAttributeNames["#title"] = "title";
        ExpressionAttributeValues[":title"] = opts.title
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#category = :category,";
        ExpressionAttributeNames["#category"] = "category";
        ExpressionAttributeValues[":category"] = opts.category
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#dimension = :dimension,";
        ExpressionAttributeNames["#dimension"] = "dimension";
        ExpressionAttributeValues[":dimension"] = opts.dimension
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#destination = :destination,";
        ExpressionAttributeNames["#destination"] = "destination";
        ExpressionAttributeValues[":destination"] = opts.destination
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#timeStart = :timeStart,";
        ExpressionAttributeNames["#timeStart"] = "timeStart";
        ExpressionAttributeValues[":timeStart"] = opts.timeStart
      }

      if ((opts && opts.category)) {
        UpdateExpression = UpdateExpression + "#timeEnd = :timeEnd,";
        ExpressionAttributeNames["#timeEnd"] = "timeEnd";
        ExpressionAttributeValues[":timeEnd"] = opts.timeEnd
      }

      if ((opts && opts.publish)) {
        UpdateExpression = UpdateExpression + "#publish = :publish,"
        ExpressionAttributeNames["#publish"] = "publish"
        ExpressionAttributeValues[":publish"] = "x";
      }


      UpdateExpression = UpdateExpression.replace(new RegExp(',$'), '');

      if ((opts && opts.nbviews)) {
        UpdateExpression = UpdateExpression + "ADD #nbviews = :nbviews"
        ExpressionAttributeNames["#nbviews"] = "nbviews"
        ExpressionAttributeValues[":nbviews"] = 1;
      }

      if ((opts && !opts.publish)) {
        UpdateExpression = UpdateExpression + " REMOVE #publish"
        ExpressionAttributeNames["#publish"] = "publish"
      }

      return this.dataService.update(TABLENAME, Key, UpdateExpression, null, ExpressionAttributeNames, ExpressionAttributeValues);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM