简体   繁体   English

如何按主键和其他条件更新项目?

[英]How to update item by primary key and other conditions?

I am trying to update item by email (HASH PK), id and verifyToken . 我想通过email (HASH PK), idverifyToken更新项目。 My query looks like this: 我的查询如下所示:

params =
  TableName: 'users'
  Key:
    email:
      S: 'example@email.com'
  AttributeUpdates:
    verified:
      Action: 'PUT'
      Value:
        BOOL: true
    verifyToken:
      Action: 'DELETE'
  ExpressionAttributeValues:
    ':id': { S: '123' }
    ':verifyToken': { S: 'XXX' }
  ConditionExpression: 'id = :id and verifyToken = :verifyToken'

dynamodb.updateItem(params)

In other words I want to update Item where email = 'example@email.com' AND id = '123' AND verifyToken = 'XXX', but I am getting following error: 换句话说,我想更新项目,其中email ='example@email.com'并且id ='123'并且verifyToken ='XXX',但是我收到以下错误:

Can not use both expression and non-expression parameters in the same request: 
Non-expression parameters: {AttributeUpdates} 
Expression parameters: {ConditionExpression}

You are combining legacy parameters ( AttributeUpdates ), which are only there for backwards compatibility, with expression parameters ( ConditionExpression ). 您正在将遗留参数( AttributeUpdates )与表达式参数( ConditionExpression )相结合,这些参数仅用于向后兼容。 As the error states, you cannot do that. 正如错误所述,您无法做到这一点。

You need to use an UpdateExpression in conjunction with your ConditionExpression . 您需要将UpdateExpressionConditionExpression结合使用。

It would be something like this. 这将是这样的。 You may need to use expression attribute names/values in the UpdateExpression : 您可能需要在UpdateExpression使用表达式属性名称/值:

ConditionExpression: 'id = :id and verifyToken = :verifyToken'
UpdateExpression: 'SET verified = true, REMOVE verifyToken'

See this documentation for more information on update expressions 有关更新表达式的更多信息,请参阅此文档

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

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