3

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It looks like it works for me now. Just an extra question: Can the sort key be auto generated somehow by dynamodb, so if I put in a record with the type "dispenser" it will be "dispenser-<dispenserID>" and vice versa for type "robot"? Or do I have to generate that key in code when putting the item?
You will have to generate it yourself. However, if you're using the DynamoDBMapper library, you could abuse the DynamoDBTypeConverter interface to convert String to String and add/remove the type prefix to the id.
2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.