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

问题描述

我的文档如下:

{
  "data": {
      "eventId": "20161029125458-df-d",
      "name": "first",
      "purpose": "test",
      "location": "yokohama",
      "dateArray": [],
      "attendees": [
        {
          "attendeeId": "2016102973634-df",
          "attendeeName": "lakshman",
          "personalizedDateSelection": {}
        },
        {
          "attendeeId": "2016102973634-tyyu",
          "attendeeName": "diwaakar",
          "personalizedDateSelection": {}
        }
      ]
    }
}

说,我需要使用 attendeeId:2016102973634-df 更新与会者JSON数组.我尝试了多种使用更新和条件表达式的方法,但没有成功.

Say, I need to update the attendee JSON array with attendeeId: 2016102973634-df. I tried many ways ways using update and condition expression, but no success.

这是我的尝试:

const params = {
  TableName: "event",
  Key: {
    "eventId": eventId
  },
  UpdateExpression: "SET attendees[???] = ",
  ConditionExpression: attendees.attendeeId = "2016102973634-df",
  ExpressionAttributeValues: {
    ":attendee" : attendeeList
  },
  ReturnValues: "ALL_NEW"
};


dynamo.update(params, (err, data) => {
  if (err) {
    return reject(err);
  }
  console.log(data.Attributes);
});

找不到用于更新数组中的Json的任何资源.

Could not find any resources for updating an Json in a array.

@notionquest发表评论后:-没有使用过任何JsonMarshaller.最初,我将空数组添加到与会者字段中,如下所示:

After @notionquest's comment:- Have not used any JsonMarshaller. Initially I added the empty array to attendees field like this:

{
  "eventId": "20161029125458-df-d",
  "name": "first",
  "purpose": "test",
  "location": "yokohama",
  "dateArray": [],
  "attendees": []
}

,然后在出现新的与会者时,将其添加到与会者属性中,如下所示:

and then When a new attendee comes I add it to the attendees property like this:

const attendee = {
  "attendeeName": "user1",
  "personalizedDateSelection": {"today": "free"}
}
const attendeeList = [attendee];
const eventId = "20161029125458-df-d";


const params = {
  TableName: "event",
  Key: {
    "eventId": eventId
  },
  UpdateExpression: "SET attendees = list_append(attendees, :attendee)",
  ExpressionAttributeValues: {
    ":attendee" : attendeeList
  },
  ReturnValues: "ALL_NEW"
};


dynamo.update(params, (err, data) => {
  if (err) {
    return reject(err);
  }
  console.log("in update dynamo");
  console.log(data.Attributes);
});

如您在上面的片段中所见,最初,我添加空的 []数组,并使用上面的代码添加新与会者.现在,如何更新数组中的特定JSON.如果您说那不可能,那么我还能尝试什么?

As you have seen in the above snippets, initially I add empty [] array and add a new attendee using the above code. Now, How do I update a specific JSON in an array. If you say that is not possible, what else can I try?

我应该试试这个吗?

  1. 获取完整的JSON.
  2. 操纵JSOn并在nodeJS中更改我想要的东西.
  3. 然后将新的JSON更新为dynamoDB.
  4. 但这消耗了对dynamoDB的两次调用,这似乎效率很低.

是否想知道是否有通行路线?

Would like to know If there is any round way ?

推荐答案

我找不到查询和更新JSON数组的任何答案.我认为这可能是AWS获利的动机,不允许这些功能.如果您需要查询除主键以外的特定ID,则需要创建一个具有成本效益的二级索引.二级索引成本是dyn的附加成本amoDB表成本.

I could not find any answer to query and update the JSON-array. I think this may be AWS profitable motive to not allow those features. If you need to query on a particular ID other than primary key, you need to make a secondary index which is cost effective. This secondary index cost is additional to the dynamoDB table cost.

因为我不想在二级索引上花额外的钱,所以我将dynamoDB模式更改为以下内容:

Since, I did not want to pay extra bucks on secondary index, I changed my dynamoDB schema to the following:

{
  "data": {
      "eventId": "20161029125458-df-d",
      "name": "first",
      "purpose": "test",
      "location": "yokohama",
      "dateArray": [],
      "attendees": {
        "2016102973634-df": {
          "attendeeId": "2016102973634-df",
          "attendeeName": "lakshman",
          "personalizedDateSelection": {}
        },
        "2016102973777-df": {
          "attendeeId": "2016102973777-df",
          "attendeeName": "ffff",
          "personalizedDateSelection": {}
        }
      }
    }
}

将参加者从 [] 更改为 {} .这使我可以灵活地查询特定的 attendeeId 并更改与之关联的整个JSON.即使这是一个多余的步骤,我也不想在我的业余爱好项目上花费额外的钱.

Changing attendees from [] to {}. This allows me the flexibility to query particular attendeeId and change the entire JSON associated with that. Even though, this is a redundant step, I do not want to spend extra bucks on my hobby project.

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

08-28 06:55