简体   繁体   English

DynamoDB:如果属性已存在,如何更新除一个属性之外的项目

[英]DynamoDB: How to update an item except for one attribute if the attribute already exists

So I'm trying to put an Item into a DDB table (nodejs).所以我试图将一个项目放入一个 DDB 表(nodejs)中。 If the Item has a 'date_created' attribute, I want to update all fields in that Item except for the 'date_created'.如果项目具有“date_created”属性,我想更新该项目中除“date_created”之外的所有字段。

I've looked at conditional expressions and from what I understand it's pretty binary.我看过条件表达式,据我所知,它是非常二元的。 If condition == true then proceed, if not, then don't.如果条件 == 真则继续,如果不是,则不要。 What I'm looking for is to do the put no matter what but don't update 'date_created' if it exists.我正在寻找的是无论如何都要进行放置,但如果存在,则不要更新“date_created”。

Is this possible?这可能吗? Am I even approaching this with the right mindset?我什至以正确的心态来解决这个问题吗?

You don't want a PutItem with a ConditionExpression for this case.在这种情况下,您不需要带有 ConditionExpression 的 PutItem。

You need to use the UpdateItem API.您需要使用UpdateItem API。 UpdateItem is an "upset", so if the primary key does not exist, the API will cause a new item to be inserted. UpdateItem 是一个“不安”,所以如果主键不存在,API 将导致插入一个新项目。 This API accepts an Update Expression which can be used to set, modify, and/or remove one or more fields from the item.此 API 接受更新表达式,可用于设置、修改和/或从项目中删除一个或多个字段。 Update Expressions have a few special functions available to them, and in order to prevent overwriting existing data, you should use if_not_exists(path, operand) .更新表达式有一些可用的特殊函数,为了防止覆盖现有数据,您应该使用if_not_exists(path, operand) If path does not exist, DynamoDB will put the result of operand at path .如果path不存在,DynamoDB 会将operand的结果放在path If path does exist, it will be unchanged.如果path确实存在,它将保持不变。

The update expression for your case would look something like this:您案例的更新表达式如下所示:

SET if_not_exists(date_created, :right_now), other_field_1 = :value_1, other_field_2 = :value_2

I don't believe DynamoDB supports this natively.我不相信 DynamoDB 本身就支持这一点。

A better solution might have date_created on initial put, and date_updated on subsequent updates.更好的解决方案可能在初始放置时使用date_created ,在后续更新时使用date_updated The initial put is a consequence of some high-level 'new item' kind of operation (like 'Add Employee').初始放置是某种高级“新项目”操作(如“添加员工”)的结果。 The subsequent updates don't need to be conditional -- they don't include date_created , just date_updated .后续更新不需要是有条件的——它们不包括date_created ,只包括date_updated And you hide those two attributes within your model layer (that implements createRecord() and updateRecord() , for example).并且您将这两个属性隐藏在模型层中(例如,实现了createRecord()updateRecord() )。

Alternatively, do an initial DDB put, including date_created , that is conditional on date_created being absent.或者,做一个初始的 DDB 放置,包括date_created ,条件是date_created不存在。 If this conditional request fails, do an update without date_created .如果此条件请求失败,请在没有date_created情况下进行更新。

I agree with @jarmod on the best way to do this;我同意@jarmod 的最佳方式; however, if you really want to do an update on all fields except the one field you can do an update instead of a put .但是,如果您真的想对除一个字段之外的所有字段进行更新,您可以执行update而不是put In JavaScript you could even generate the update statement based on the keys in the data, and exclude the date_created value if it happens to be in the data.在 JavaScript 中,您甚至可以根据数据中的键生成更新语句,并排除date_created值(如果它恰好在数据中)。

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

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