Dynamodb-更新对象数组中JSON对象的值 [英] Dynamodb - Update value of JSON object in array of objects

查看:97
本文介绍了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对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆