将对象添加到 dynamo db 中的地图

add object to map in dynamo db

我正在尝试向 dynamo 数据库中的现有 MAP 对象添加一个元素,但由于某些原因,当我 运行 添加一个新列时,我改为添加了一个新列。这个查询有什么问题?我的 table 结构是:

topic      questions
-----      ----------
Dynamo     { "what is Dynamo?" : { "S" : "x" }  }

代码:

var params = {
  TableName: TABLE_NAME,
  Key: { topic: topic },
  UpdateExpression: "ADD #qq = :ans",
  ExpressionAttributeNames: {
    "#qq": qq,
  },
  ExpressionAttributeValues: {
    ":ans": { S: ans },
  },
};
let res = await docClient.update(params).promise();

ADD用于数字和Set,不用于MAP。

要将元素附加到 MAP,我们需要使用 SET

const TABLE_NAME = "test";
const topic = "Dynamo";
const qq = "what is Dynamo?";
const answer = "its a nosql database";
docClient.update(
  {
    TableName: TABLE_NAME,
    Key: {
      topic: topic,
    },
    UpdateExpression: "set questions.#qq = :answer",
    ExpressionAttributeValues: {
      ":answer": answer,
    },
    ExpressionAttributeNames: {
      "#qq": qq,
    },
  },
  function (error, result) {
    console.log("error", error, "result", result);
  }
);