0
0
DynamodbConceptBeginner · 3 min read

What is Map Type in DynamoDB: Explanation and Example

In DynamoDB, the Map type is a data structure that stores a collection of key-value pairs, where each key is a string and the value can be any DynamoDB data type. It works like a small dictionary or object inside a single attribute, allowing you to group related data together within one item.
⚙️

How It Works

The Map type in DynamoDB is similar to a mini folder inside a database item that holds multiple labeled pieces of information. Imagine you have a box labeled "Address" and inside it, you keep smaller boxes labeled "Street", "City", and "Zip Code". Each smaller box holds a value, and together they form the full address.

This lets you organize complex data neatly inside one attribute instead of spreading it across many attributes. Each key in the map is a string, and the value can be simple like a number or string, or even another map or list, allowing for nested structures.

💻

Example

This example shows how to create a DynamoDB item with a Map attribute named Profile that holds a user's details.

python
import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Users')

item = {
    'UserId': 'user123',
    'Profile': {
        'Name': 'Alice',
        'Age': 30,
        'Contact': {
            'Email': 'alice@example.com',
            'Phone': '123-456-7890'
        }
    }
}

table.put_item(Item=item)

response = table.get_item(Key={'UserId': 'user123'})
print(response['Item'])
Output
{"UserId": "user123", "Profile": {"Name": "Alice", "Age": 30, "Contact": {"Email": "alice@example.com", "Phone": "123-456-7890"}}}
🎯

When to Use

Use the Map type when you want to group related data together inside a single attribute. This is helpful when the data naturally forms a structure, like an address, user profile, or settings.

For example, if you store user information, you can keep all contact details inside a Map instead of separate attributes. This makes your data cleaner and easier to manage.

Maps are also useful when you want to store flexible or nested data without creating many separate attributes or tables.

Key Points

  • Map stores key-value pairs inside one attribute.
  • Keys are always strings; values can be any DynamoDB type.
  • Maps can be nested inside other maps or lists.
  • Useful for grouping related data like profiles or addresses.
  • Helps keep data organized and flexible.

Key Takeaways

The Map type in DynamoDB stores related key-value pairs inside a single attribute.
Maps allow nesting, so you can create complex, structured data easily.
Use Maps to group data like user profiles or settings for cleaner design.
Each key in a Map is a string, and values can be any DynamoDB data type.
Maps help keep your data organized and flexible without many separate attributes.