Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating indexes from nested structure in DynamoDB

I wonder if it's possible to create an index that could look like this

{
  "dispenserId": "my-dispenser-123",  // primary key
  "users": ["user5", "user12"],
  "robotId": "my-robot-1",
  "enabled": true,
  "side": left
}

Based on my DynamoDB documents that look like this

{
  "robotId": "my-robot-1",    // primary key
  "dispensers": {
    "left": "left-dispenser-123",
    "right": "right-dispenser-123",
    "users": ["user5", "user12"]
  },
  "enabled": true,
  "users": ["user1", "user32"]
}

I can't figure out how to point at either dispensers.left or dispensers.right and use that as a key, neither can I figure out how to make a side: left/right attribute based on the path of the dispenser ID.

Can it be achieved with the current structure? If not, what document structure would you guys suggest instead. which allows me to hold the same data?

like image 303
Esben von Buchwald Avatar asked Mar 08 '26 02:03

Esben von Buchwald


2 Answers

What you are trying to do (use a map element as a key attribute for an index) is not supported by DynamoDB.

The index partition key and sort key (if present) can be any base table attributes of type string, number, or binary. (Source)

You cannot use (an element of) a map attribute as a a key attribute for an index because the key attribute must be a string, number, or binary attribute from the base table.

Consider using the adjacency list design pattern for your data. It will allow you to easily add both the left and right dispensers to your index.

like image 66
Matthew Pope Avatar answered Mar 09 '26 16:03

Matthew Pope


My new structure looks like this

partition key: robotId
sort key: compoundKey

[
  {
    "robotId": "robot1", 
    "enabled": true, 
    "users": [
      "user1", 
      "user3"
    ], 
    "compositeKey": "robot--robot1"
  }, 
  {
    "robotId": "robot1", 
    "dispenserId": "dispenser1", 
    "compositeKey": "dispenser--dispenser1", 
    "side": "left", 
    "users": [
      "user4", 
      "user61"
    ]
  }
]

Then I have an index with the dispenserId as partition key, so I can either look the dispensers for a given robot (using the table) or look up details about a dispenser (using the index)

like image 43
Esben von Buchwald Avatar answered Mar 09 '26 17:03

Esben von Buchwald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!