2017-05-06 44 views
0

如何使用AWS SDK for C#更新項目?如何更新DynamoDB中列表中的項目?

可以說,我想更新「選定」的值改爲「真」時ITEMNAME等於「EQQ」。

我寫了這個代碼沒有成功:在DynamoDB表

request = { 
    ExpressionAttributeNames = new Dictionary<string, string>() 
    { 
     {"#I", "Items"}, 
     {"#lN", item.ItemName} 
    }, 
    ExpressionAttributeValues = new Dictionary<string, AttributeValue>() 
    { 
     { ":item", new AttributeValue 
      { L = new List<AttributeValue> { 
       { new AttributeValue 
        { M = new Dictionary<string,AttributeValue> { 
         { "ListName", new AttributeValue { S = "item.ItemName"} }, 
         { "Selected", new AttributeValue { BOOL = item.Selected} }, 
         { "ImageSource", new AttributeValue { S = item.ImageSource} } 
        }} 
       } 
      }} 
     } 
    }, 
    UpdateExpression = "SET #I.#lN = :item" 
    // UpdateExpression = "SET #I = list_append(:item,#I)" 
    // UpdateExpression = "SET #I = :item" 
}; 
var response = await client.UpdateItemAsync(request); 

我的JSON數據遵循以下結構:

{ 
    "Items": [ 
     { 
      "ImageSource": "checked.png", 
      "ItemName": "egg", 
      "Selected": true 
     }, 
     { 
      "ImageSource": "checked.png", 
      "ItemName": "Water", 
      "Selected": true 
     } 
    ], 
    "ListCategory": "Technology", 
    "ListCreator": "John", 
    "ListId": "e5a7ec9d-b00c-41f3-958a-84c8c183d702", 
    "ListName": "Test5", 
    "UpdateDateTıme": "2017-05-05T21:48:41.833Z" 
} 

回答

1

你無法預先知道這在列表項將包含ItemName蛋。您可以閱讀該項目,然後在ItemName = egg的Items列表中的第0個項目上進行調整。這個策略需要你先閱讀這個項目,這樣你才能知道雞蛋在列表中的位置。否則,你可以窩在一張地圖中的項目:

{ 
    "ItemMap": { 
     "egg":{ 
      "ImageSource": "checked.png", 
      "Selected": true 
     }, 
     "Water": { 
      "ImageSource": "checked.png", 
      "Selected": true 
     } 
    }, 
    "ListCategory": "Technology", 
    "ListCreator": "John", 
    "ListId": "e5a7ec9d-b00c-41f3-958a-84c8c183d702", 
    "ListName": "Test5", 
    "UpdateDateTıme": "2017-05-05T21:48:41.833Z" 
} 

,並使用以下表達式:

  1. UpdateExpression = ItemMap.egg.Selected = :bv
  2. ExpressionAttributeValues = {:bv: true}
  3. ConditionExpression = attribute_exists(ItemMap.egg) AND attribute_type(ItemMap.egg, M)
+0

謝謝你,它是個好主意:)這就是我正在尋找的。 –