What is String Type in DynamoDB: Explanation and Example
String type represents text data stored as Unicode strings. It is used to store words, sentences, or any sequence of characters, similar to text fields in other databases. DynamoDB strings are case-sensitive and can be up to 400 KB in size.How It Works
Think of the String type in DynamoDB as a labeled box where you can keep any text you want, like names, descriptions, or IDs. This box holds characters exactly as you type them, including spaces and punctuation, and it remembers uppercase and lowercase letters as different.
When you save a string in DynamoDB, it stores the exact sequence of characters you provide. This is useful when you want to search or filter data based on text, like finding a user by their username or filtering products by category names.
Example
This example shows how to put an item with a string attribute into a DynamoDB table using AWS SDK for JavaScript (v3).
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({ region: "us-east-1" }); async function putItem() { const params = { TableName: "Users", Item: { "UserId": { S: "user123" }, "Name": { S: "Alice" }, "Email": { S: "alice@example.com" } } }; try { const data = await client.send(new PutItemCommand(params)); console.log("Item added successfully", data); } catch (err) { console.error("Error", err); } } putItem();
When to Use
Use the String type in DynamoDB whenever you need to store text data such as names, email addresses, descriptions, or any other readable information. It is ideal for fields where the data is naturally text and you might want to search or filter based on that text.
For example, you can use strings to store usernames in a user database, product names in an inventory, or tags for categorizing items. Since DynamoDB strings are case-sensitive, be mindful if your application requires case-insensitive searches.
Key Points
- String type stores Unicode text data.
- Maximum size per string attribute is 400 KB.
- Strings are case-sensitive in DynamoDB.
- Used for text fields like names, emails, and descriptions.
- Supports filtering and querying based on string values.