简体   繁体   English

DynamoDB update_item覆盖项目

[英]DynamoDB update_item overwriting items

Update Item is overwriting my item instead of adding a new one to the list. 更新项目会覆盖我的项目,而不是将新项目添加到列表中。 Following the directions here , I should get a updated list. 按照这里的指示,我应该获得更新的列表。

My Code: 我的代码:

dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")

table = dynamodb.Table('WishListTest')

device = input('What is the Item being requested?\n')
device = device.upper()

aliasInput = input('What is the Alias of the user?\n')
aliasInput = aliasInput.upper()

date = int((time.strftime("%d%m%Y")))
response = table.update_item(
    Key={
        'Device': device,
    },
    UpdateExpression="set RequestList.Alias = :r, RequestList.Createddate = :d, AvailableQuanity = :a, "
                 "ReserveQuanity = :q",
    ExpressionAttributeValues={
        ':r': aliasInput,
        ':d': date,
        ':a': 0,
        ':q': 0
    }
)
print("PutItem succeeded:")
print(json.dumps(response, indent=4, cls=DecimalEncoder))

My output looks like this: 我的输出如下所示:

{"ReserveQuanity": 0, "AvailableQuanity": 0, "Device": "DELL", "RequestList": {"CreatedDate": 19122017, "Alias": "JONJP", "Createddate": 27122017}}

It overwrites it so long as I maintain the same Device name. 只要我保持相同的设备名称,它就会覆盖它。 Which it is supposed to just add another Alias , Createddate , ReserveQuanity , and AvailableQuanity to the end. 它应该只是在Createddate添加另一个AliasCreateddateReserveQuanityAvailableQuanity

First of all you need to ensure that all the attributes you want to add are either lists or sets . 首先,您需要确保要添加的所有属性都是列表或集合

If you use a list you need to use the list_append function in the update expression, as stated in the boto3 documentation . 如果使用列表,则需要在更新表达式中使用list_append函数,如boto3文档中所述 Otherwise you'd just overwrite the pre-existing value as you do right now: 否则,您将像现在一样覆盖先前存在的值:

SET - Adds one or more attributes and values to an item. SET向一个项目添加一个或多个属性和值。 If any of these attribute already exist, they are replaced by the new values. 如果这些属性中的任何一个已经存在,则将它们替换为新值。 You can also use SET to add or subtract from an attribute that is of type Number. 您也可以使用SET从Number类型的属性中添加或减去。 For example: SET myNum = myNum + :val SET supports the following functions: 例如: SET myNum = myNum + :val SET支持以下功能:

  • if_not_exists (path, operand) - if the item does not contain an attribute at the specified path, then if_not_exists evaluates to operand; if_not_exists (path, operand) -如果该项在指定路径上不包含属性,则if_not_exists求值为操作数; otherwise, it evaluates to path. 否则,它将评估路径。 You can use this function to avoid overwriting an attribute that may already be present in the item. 您可以使用此功能来避免覆盖项目中可能已经存在的属性。
  • list_append (operand, operand) - evaluates to a list with a new element added to it. list_append (operand, operand) -评估为一个列表,其中添加了新元素。 You can append the new element to the start or the end of the list by reversing the order of the operands. 您可以通过反转操作数的顺序将新元素追加到列表的开头或结尾。

If you use a set you don't need the list_append function, but have to use the ADD action instead of the SET action: 如果使用集合,则不需要list_append函数,而必须使用ADD操作而不是SET操作:

ADD - Adds the specified value to the item, if the attribute does not already exist. ADD如果属性不存在,则将指定的值添加到项目。 If the attribute does exist, then the behavior of ADD depends on the data type of the attribute: 如果该属性确实存在,则ADD的行为取决于该属性的数据类型:

  • If the existing data type is a set and if Value is also a set, then Value is added to the existing set. 如果现有数据类型是集合,并且“ Value也是集合,则将“ Value添加到现有集合。
  • If the existing data type is a set and if Value is also a set, then Value is added to the existing set. 如果现有数据类型是集合,并且“ Value也是集合,则将“ Value添加到现有集合。 For example, if the attribute value is the set [1,2] , and the ADD action specified [3] , then the final attribute value is [1,2,3] . 例如,如果属性值是集合[1,2] ,并且ADD操作指定为[3] ,则最终属性值是[1,2,3] An error occurs if an ADD action is specified for a set attribute and the attribute type specified does not match the existing set type. 如果为集合属性指定了ADD操作,并且指定的属性类型与现有集合类型不匹配,则会发生错误。 Both sets must have the same primitive data type. 两组都必须具有相同的原始数据类型。 For example, if the existing data type is a set of strings, the Value must also be a set of strings. 例如,如果现有数据类型是一组字符串,则“ Value也必须是一组字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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