Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html), but I can't seem to see any examples, beyond removing items from a List by index value....

like image 817
Victor S Avatar asked Dec 02 '15 19:12

Victor S


1 Answers

I've run into this issue a few times now, and unfortunately there's no straightforward solution. The two most common workarounds I've come 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.com/amazondynamodb/latest/developerguide/GuidelinesForItems.html

like image 137
Matty F Avatar answered Sep 19 '22 14:09

Matty F