2017-05-06 46 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

谢谢你,它是个好主意:)这就是我正在寻找的。 –