为什么 Dynamo DB 在更新具有相同值的映射键时会抛出错误?

Why does Dynamo DB throw an error when updating a map key with the same value?

我正在尝试 运行 一个简单的更新查询,但是当我尝试将映射的键更新为相同的值时出现错误。是否有技术原因不允许这样做?或者我这样做违反了某种最佳实践?

错误:

ValidationException: Invalid UpdateExpression: Two document paths overlap with each other; 
must remove or rewrite one of these paths; path one: [questions, What is xx?], path two: [questions, What is xx?]

查询对象:

{
  TableName: 'notesTable',
  Key: { topic: 'My tooic' },
  ExpressionAttributeNames: { '#qq': 'What is xx?', '#updq': 'What is xx?' },
  ExpressionAttributeValues: { ':updans': 'new answer' },
  UpdateExpression: 'REMOVE questions.#qq SET questions.#updq = :updans'
}

多种方法来处理需要更新相同密钥的情况。我们可以简单地设置 key ,而不是删除和更新相同的键,无论如何它都会替换值。

所以,简单的方法就是每次发送不同的updateExpression。

const qq = "What is xx2?";
const updq = "What is xx?";
let expressionAttributeNames;
let UpdateExpression;

if (qq === updq) {
  expressionAttributeNames = { "#updq": "What is xx?" };
  UpdateExpression = "SET questions.#updq = :updans";
} else {
  expressionAttributeNames = { "#qq": "What is xx1?", "#updq": "What is xx?" };
  UpdateExpression = "REMOVE questions.#qq SET questions.#updq = :updans";
}

docClient.update(
  {
    TableName: "test",
    Key: {
      id: "My tooic",
    },
    ExpressionAttributeNames: expressionAttributeNames,
    ExpressionAttributeValues: { ":updans": "new answer1" },
    UpdateExpression: UpdateExpression,
  },
  function (error, result) {
    console.log("error", error, "result", result);
  }
);