最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I delete from DynamoDB List of Maps, by Map attribute value in said List? - Stack Overflow

programmeradmin0浏览0评论

I have the following List example:

{
  "favorites": [
    {
      "createdAt": 1448998673852,
      "entityId": "558da3de395b1aee2d6b7d2b",
      "type": "media"
    },
    {
      "createdAt": 1448998789252,
      "entityId": "558da3de395b1aee2d6b7d83",
      "type": "media"
    },
    {
      "createdAt": 1448998793729,
      "entityId": "558da3de395b1aee2d6b7d99",
      "type": "media"
    },
    {
      "createdAt": 1448998813023,
      "entityId": "558da3de395b1aee2d6b7daf",
      "type": "media"
    }
  ],
  "userId": "2"
}

And I want to remove the Map with: "entityId": "558da3de395b1aee2d6b7d2b".

I am thinking an UpdateItem query, I have looked at REMOVE expression documentation (.Modifying.html), but I can't seem to see any examples, beyond removing items from a List by index value....

I have the following List example:

{
  "favorites": [
    {
      "createdAt": 1448998673852,
      "entityId": "558da3de395b1aee2d6b7d2b",
      "type": "media"
    },
    {
      "createdAt": 1448998789252,
      "entityId": "558da3de395b1aee2d6b7d83",
      "type": "media"
    },
    {
      "createdAt": 1448998793729,
      "entityId": "558da3de395b1aee2d6b7d99",
      "type": "media"
    },
    {
      "createdAt": 1448998813023,
      "entityId": "558da3de395b1aee2d6b7daf",
      "type": "media"
    }
  ],
  "userId": "2"
}

And I want to remove the Map with: "entityId": "558da3de395b1aee2d6b7d2b".

I am thinking an UpdateItem query, I have looked at REMOVE expression documentation (http://docs.aws.amazon./amazondynamodb/latest/developerguide/Expressions.Modifying.html), but I can't seem to see any examples, beyond removing items from a List by index value....

Share Improve this question asked Dec 2, 2015 at 19:45 Victor SVictor S 5,1425 gold badges46 silver badges63 bronze badges 2
  • I am facing similar issue. Did you figure out a solution for this? – ferrari16 Commented Apr 5, 2017 at 19:12
  • Ditto. I'm having trouble as well. – ephemeralCoder Commented Apr 11, 2017 at 17:39
Add a ment  | 

3 Answers 3

Reset to default 2

I've run into this issue a few times now, and unfortunately there's no straightforward solution. The two most mon workarounds I've e across are:

A. use a map instead of a list, you can reference the favorite directly in your update expression now using #favorites.#entityId. If order is important, add an order attribute to each map. In your data access layer is where you would need to do your conversions to/from the desired array type.

{
  "userId": "2",
  "favorites": {
    "558da3de395b1aee2d6b7d2b": {
      "createdAt": 1448998673852,
      "entityId": "558da3de395b1aee2d6b7d2b",
      "type": "media",
    },
    // ... more entities here
  }
}

B. use one-to-many tables instead, see http://docs.aws.amazon./amazondynamodb/latest/developerguide/GuidelinesForItems.html

I have done this before in Python using the Boto3 library but not in Javascript. I have a link to the Javascript delete item function. What I did in Python was that I queried my table for the item(s) and make a list of the primary keys of the objects I wanted deleted and called the delete function.

You should be de serializing the object into something you can work with, modifying it to remove that entry, then using update-item to save the changes.

I'm not a node.js user, but how did you create the object in the first place? Are you hand crafting the json string? If so you shouldn't be. There are libraries to handle json, and making it easy for you to modify them, and then get the resulting json string with the changes.

How to parse JSON using Node.js?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论