dorbmd1177 2016-04-08 18:33 采纳率: 100%
浏览 33

dynamodb:有条件地创建/更新项目并返回当前值

Is it possible in dynamoDB to conditionally create/update an item and return its current value?

In the below we have three states, one in which the user is not in the table yet, one in which the user is in the table but does not have a CreatedAt field, and one in which the user exists and has a CreatedAt field.

out, err := db.UpdateItem(&dynamodb.UpdateItemInput{
  TableName: aws.String("User"),
  Key: map[string]*dynamodb.AttributeValue{
    "ID": {S: aws.String(u.ID)},
  },
  ExpressionAttributeNames: map[string]*string{
    "#c": aws.String("CreatedAt"),
  },
  ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
    ":c": {S: aws.String(u.CreatedAt.Format(time.RFC3339))},
  }
  ConditionExpression: aws.String("attribute_not_exists(#c)"),
  UpdateExpression:    aws.String("SET #c = :c"),
  ReturnValues:        aws.String("ALL_NEW"),
})
if isConditionError(err) {
  // out.Attributes == nil
  // .: out.Attributes["CreatedAt"] == ""
}

When the user is not present, this creates the user as expected.

When the user does not have a CreatedAt field, it adds that field as expected.

When the condition fails (i.e. the user has a value for CreatedAt), I would like to know what the database says in the CreatedAt field for that user. Unfortunately regardless whether I specify ALL_NEW or ALL_OLD for ReturnValues, out.Attributes is always nil.

I would like to avoid calling the database twice to get this information, and since I know dynamo is reading it to evaluate the condition I think this is reasonable. How would I do this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 目详情-五一模拟赛详情页
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line