Bird
Raised Fist0
DynamoDBquery~10 mins

Attribute types (S, N, B, BOOL, L, M) in DynamoDB - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Attribute types (S, N, B, BOOL, L, M)
Start with attribute
Check attribute type
S
Store as string
Store as number
Store as binary
Store as boolean
Store as list
Store as map
Reject invalid type
The attribute type is checked and stored accordingly: string (S), number (N), binary (B), boolean (BOOL), list (L), or map (M). Invalid types are rejected.
Execution Sample
DynamoDB
Item = {
  "Name": {"S": "Alice"},
  "Age": {"N": "30"},
  "IsMember": {"BOOL": true},
  "Tags": {"L": [{"S": "VIP"}, {"S": "Premium"}]},
  "Details": {"M": {"Height": {"N": "170"}}}
}
This code defines a DynamoDB item with attributes of different types: string, number, boolean, list, and map.
Execution Table
StepAttributeType DetectedActionStored Format
1NameSStore as string{"S": "Alice"}
2AgeNStore as number{"N": "30"}
3IsMemberBOOLStore as boolean{"BOOL": true}
4TagsLStore as list{"L": [{"S": "VIP"}, {"S": "Premium"}]}
5DetailsMStore as map{"M": {"Height": {"N": "170"}}}
6UnknownAttrXReject invalid typeError: Invalid attribute type
💡 All attributes processed; invalid type rejected at step 6.
Variable Tracker
AttributeInitialAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
Nameundefined{"S": "Alice"}{"S": "Alice"}{"S": "Alice"}{"S": "Alice"}{"S": "Alice"}{"S": "Alice"}
Ageundefinedundefined{"N": "30"}{"N": "30"}{"N": "30"}{"N": "30"}{"N": "30"}
IsMemberundefinedundefinedundefined{"BOOL": true}{"BOOL": true}{"BOOL": true}{"BOOL": true}
Tagsundefinedundefinedundefinedundefined{"L": [{"S": "VIP"}, {"S": "Premium"}]}{"L": [{"S": "VIP"}, {"S": "Premium"}]}{"L": [{"S": "VIP"}, {"S": "Premium"}]}
Detailsundefinedundefinedundefinedundefinedundefined{"M": {"Height": {"N": "170"}}}{"M": {"Height": {"N": "170"}}}
UnknownAttrundefinedundefinedundefinedundefinedundefinedundefinedError: Invalid attribute type
Key Moments - 3 Insights
Why is the number attribute stored as a string in the 'N' type?
In DynamoDB, numbers are stored as strings to preserve precision and avoid floating-point errors, as shown in execution_table step 2.
What happens if an attribute type is not one of S, N, B, BOOL, L, or M?
The attribute is rejected as invalid, demonstrated in execution_table step 6 where 'UnknownAttr' causes an error.
How are complex data structures like lists and maps stored?
Lists (L) store ordered collections of attributes, and maps (M) store key-value pairs of attributes, as seen in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the stored format of the 'IsMember' attribute?
A{"BOOL": true}
B{"S": "true"}
C{"N": "1"}
Dtrue
💡 Hint
Check the 'Stored Format' column for step 3 in the execution_table.
At which step does the process reject an invalid attribute type?
AStep 2
BStep 4
CStep 6
DStep 5
💡 Hint
Look for the row with 'Reject invalid type' in the 'Action' column.
If the 'Age' attribute was given as {"N": 30} (number, not string), how would the execution_table change?
AStep 2 would store as string with value "30"
BStep 2 would show an error for invalid type
CStep 2 would store as number with value 30
DNo change; DynamoDB accepts number type directly
💡 Hint
DynamoDB requires numbers as strings; see step 2 where number is stored as string.
Concept Snapshot
DynamoDB attribute types:
- S: String stored as string
- N: Number stored as string
- B: Binary data
- BOOL: Boolean true/false
- L: List of attributes
- M: Map (key-value pairs)
Invalid types cause errors.
Numbers must be strings to keep precision.
Full Transcript
This visual execution shows how DynamoDB processes attribute types. Each attribute is checked for its type: string (S), number (N), binary (B), boolean (BOOL), list (L), or map (M). The attribute is then stored in the corresponding format. Numbers are stored as strings to avoid precision loss. Lists and maps allow complex nested data. If an attribute type is invalid, it is rejected with an error. The execution table traces each attribute step by step, showing the stored format and actions taken. Variable tracking shows how each attribute's value is set after each step. Key moments clarify common confusions about number storage, invalid types, and complex structures. The quiz tests understanding of stored formats, error detection, and type requirements. The snapshot summarizes the attribute types and key rules for DynamoDB.

Practice

(1/5)
1.

Which DynamoDB attribute type is used to store plain text strings?

easy
A. B
B. N
C. BOOL
D. S

Solution

  1. Step 1: Understand attribute types in DynamoDB

    DynamoDB uses attribute types to define data kinds. 'S' stands for string (text).
  2. Step 2: Match the type to text data

    Since 'S' means string, it is the correct type for plain text.
  3. Final Answer:

    S -> Option D
  4. Quick Check:

    Text data = S [OK]
Hint: Text data always uses type S in DynamoDB [OK]
Common Mistakes:
  • Confusing N (number) with S (string)
  • Choosing BOOL for text
  • Using B for text data
2.

Which of the following is the correct syntax to define a boolean attribute named isActive in a DynamoDB item?

{"isActive": {"BOOL": true}}
easy
A. {"isActive": {"S": "true"}}
B. {"isActive": {"BOOL": true}}
C. {"isActive": {"N": 1}}
D. {"isActive": {"B": true}}

Solution

  1. Step 1: Identify the correct attribute type for boolean

    Boolean values in DynamoDB use the BOOL type with true or false.
  2. Step 2: Check the syntax for boolean attribute

    The correct syntax is {"isActive": {"BOOL": true}} to represent a boolean true.
  3. Final Answer:

    {"isActive": {"BOOL": true}} -> Option B
  4. Quick Check:

    Boolean attribute uses BOOL type [OK]
Hint: Use BOOL type with true/false for boolean values [OK]
Common Mistakes:
  • Using S type with string 'true'
  • Using N type with 1 instead of BOOL
  • Using B type for boolean
3.

Given the following DynamoDB item attribute, what is the type of tags?

{"tags": {"L": [ {"S": "red"}, {"S": "blue"}, {"S": "green"} ] }}
medium
A. List (L)
B. Map (M)
C. String (S)
D. Binary (B)

Solution

  1. Step 1: Identify the attribute type key

    The attribute uses the key "L", which stands for List in DynamoDB.
  2. Step 2: Understand the value structure

    The value is an array of strings, confirming it is a list of string elements.
  3. Final Answer:

    List (L) -> Option A
  4. Quick Check:

    Attribute key L means List [OK]
Hint: Look for L key to identify lists in DynamoDB [OK]
Common Mistakes:
  • Confusing L with M (map)
  • Thinking it's a string because of S inside list
  • Choosing B for binary data
4.

Identify the error in this DynamoDB attribute definition:

{"profile": {"M": {"age": {"S": 30}, "name": {"S": "Alice"}}}}
medium
A. The name attribute should use type N, not S
B. The map (M) type cannot contain nested attributes
C. The age attribute should use type N, not S
D. Binary (B) type is missing for profile

Solution

  1. Step 1: Analyze the attribute types inside the map

    The map contains "age" with type S and value 30, and "name" with type S and value "Alice".
  2. Step 2: Check if types match values

    Age is a number (30), so it should use type N, not S (string). Name is correctly a string.
  3. Final Answer:

    Age attribute should use type N, not S -> Option C
  4. Quick Check:

    Numbers use N type, not S [OK]
Hint: Numbers inside maps must use N type, not S [OK]
Common Mistakes:
  • Using S for numbers
  • Thinking maps can't nest attributes
  • Confusing name type as number
5.

You want to store a user's profile with a list of phone numbers and a map of address details in DynamoDB. Which attribute types should you use for phoneNumbers and address respectively?

hard
A. phoneNumbers: L, address: M
B. phoneNumbers: M, address: L
C. phoneNumbers: S, address: B
D. phoneNumbers: BOOL, address: N

Solution

  1. Step 1: Identify the type for a list of phone numbers

    A list of phone numbers is a collection, so it should use the List (L) type.
  2. Step 2: Identify the type for address details as key-value pairs

    Address details are structured data with keys and values, so Map (M) type is appropriate.
  3. Final Answer:

    phoneNumbers: L, address: M -> Option A
  4. Quick Check:

    Lists = L, Maps = M [OK]
Hint: Use L for lists, M for maps (objects) in DynamoDB [OK]
Common Mistakes:
  • Swapping L and M types
  • Using S for lists
  • Using BOOL or N incorrectly