You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
DynamoDB provides a rich set of API operations for reading and writing data. This lesson covers the core operations — PutItem, GetItem, UpdateItem, DeleteItem, Query, and Scan — along with conditional expressions, batch operations, and transactions.
Creates a new item or replaces an existing item with the same primary key:
aws dynamodb put-item \
--table-name Users \
--item '{
"UserId": {"S": "user-001"},
"Name": {"S": "Alice"},
"Email": {"S": "alice@example.com"},
"Age": {"N": "30"}
}'
Important: PutItem replaces the entire item. If an item with the same key exists, all attributes are overwritten.
Modifies specific attributes of an existing item (or creates it if it does not exist):
aws dynamodb update-item \
--table-name Users \
--key '{"UserId": {"S": "user-001"}}' \
--update-expression "SET Age = :newAge, UpdatedAt = :ts" \
--expression-attribute-values '{
":newAge": {"N": "31"},
":ts": {"S": "2024-03-04T12:00:00Z"}
}'
| Action | Description | Example |
|---|---|---|
| SET | Add or update attributes | SET Name = :name |
| REMOVE | Delete attributes | REMOVE OldField |
| ADD | Increment numbers or add to sets | ADD ViewCount :inc |
| DELETE | Remove elements from sets | DELETE Tags :tagsToRemove |
Deletes an item by its primary key:
aws dynamodb delete-item \
--table-name Users \
--key '{"UserId": {"S": "user-001"}}'
Retrieves a single item by its full primary key:
aws dynamodb get-item \
--table-name Users \
--key '{"UserId": {"S": "user-001"}}' \
--consistent-read
| Parameter | Description |
|---|---|
| --consistent-read | Strongly consistent read (default is eventually consistent) |
| --projection-expression | Return only specific attributes |
Retrieves multiple items from a single partition using the partition key and optional sort key conditions:
aws dynamodb query \
--table-name Orders \
--key-condition-expression "CustomerId = :cid AND OrderDate BETWEEN :start AND :end" \
--expression-attribute-values '{
":cid": {"S": "cust-001"},
":start": {"S": "2024-01-01"},
":end": {"S": "2024-03-31"}
}'
Key points about Query:
--scan-index-forward false for descending)Reads every item in a table:
aws dynamodb scan \
--table-name Users \
--filter-expression "Age > :minAge" \
--expression-attribute-values '{":minAge": {"N": "25"}}'
Warning: Scan reads the entire table and then applies the filter. It consumes read capacity for every item examined, not just those returned. Avoid Scan in production whenever possible — use Query or indexes instead.
Add conditions to write operations to implement optimistic locking and business rules:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.