Data Types in DynamoDB: Overview and Examples
String, Number, and Binary. These types help DynamoDB understand how to store, index, and retrieve your data efficiently. Common types include S for strings, N for numbers, and B for binary data.How It Works
DynamoDB stores data in tables, where each item (like a row) has attributes (like columns). Each attribute has a data type that tells DynamoDB what kind of value it holds. Think of data types as labels on boxes that tell you what is inside, so you know how to handle them properly.
For example, a String type holds text like names or addresses, while a Number type holds numeric values like prices or counts. DynamoDB also supports complex types like lists and maps, which let you store multiple values or nested data inside one attribute, similar to how a folder can hold many files.
This system helps DynamoDB organize and query your data quickly and correctly, just like sorting mail by type helps deliver it faster.
Example
This example shows how to define different data types in a DynamoDB item using JSON format for the AWS SDK.
{
"TableName": "Products",
"Item": {
"ProductId": { "S": "123" },
"Price": { "N": "19.99" },
"InStock": { "BOOL": true },
"Tags": { "SS": ["sale", "new"] },
"Details": { "M": {
"Weight": { "N": "1.5" },
"Color": { "S": "red" }
}}
}
}When to Use
Use DynamoDB data types to match the kind of data you want to store. Use String for text like usernames or emails, Number for quantities or prices, and Boolean for true/false flags.
Use sets (SS, NS, BS) when you want to store unique lists of strings, numbers, or binary data, like tags or IDs. Use List and Map types to store complex or nested data, such as product details or user preferences.
This helps keep your data organized and makes queries efficient, especially in applications like e-commerce, gaming, or IoT where data types vary.
Key Points
- DynamoDB supports simple types like
String,Number,Binary, andBoolean. - It also supports sets of strings, numbers, or binary data for unique collections.
- Complex types like
ListandMapallow nested or multiple values. - Choosing the right data type helps DynamoDB store and query data efficiently.
- Data types are specified explicitly when adding or updating items.