本文介绍了Dynamodb - 更新对象数组中 JSON 对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是存储在 DynamoDb 中的示例项目对象/记录.我使用 NodeJS 和 AWS.DynamoDB.DocumentClient 来访问数据库.

Below is a sample item object/record stored in DynamoDb. I use NodeJS and AWS.DynamoDB.DocumentClient to access the database.

我正在构建一个 PUT 函数来更新数组中 JSON 对象的状态.该函数可以访问项目的 uuid 和房间的 uuid.给定 JSON 对象数组,如何简单(创造性地)更新相应 status 字段的值?

I'm building out a PUT function to update the status for an JSON object in an array. The function will have access to the Item's uuid and room's uuid. How can I simply (creatively) update the value of corresponding status field, given the array of JSON objects?

参数:

let params = {
  TableName: room-table,
  Key: {
    uuid: event.body.uuid
  },
  UpdateExpression : "??",
  ExpressionAttributeNames: {
      "??":"??"
  },
  ExpressionAttributeValues:{
    "??":"??"
  },
  ReturnValues:"ALL_NEW"
};

物品对象:

{
  "Item": {
    "uuid": "77b1e88e-5e60-44d9-b6ca-aec345c0dc99",
    "rooms": [
      {
        "room": "303",
        "status": "pending",
        "uuid": "b8f1c1a8-04a9-4c2e-82ad-bc3e81face35"
      },
      {
        "room": "302",
        "status": "pending",
        "uuid": "42fdc61a-4a25-4316-90c9-60209875d208"
      },
      {
        "room": "678",
        "status": "pending",
        "uuid": "7bedc115-20ed-4c3e-9cd7-7fed0520f4df"
      }
    ],
    "status": "pending"
  }
}

推荐答案

使用 ExpressionAttributeValues 无法做到这一点.我必须构建一个函数来修改对象,如下所示:

It's not possible to do this with ExpressionAttributeValues. I had to build a function to modify the object, similar to below:

function setStatus(jsonObj, uuid, newStatus) {
  for (var i=0; i<jsonObj.length; i++) {
    if (jsonObj[i].uuid === uuid) {
      jsonObj[i].status = newStatus;
      return jsonObj;
    }
  }
}

这篇关于Dynamodb - 更新对象数组中 JSON 对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:49