Manage Items

Create a New Item

Use the form below to create a new item in your selected container. Items in Cosmos DB are typically represented as JSON documents.

Example Item Creation
```python
from azure.cosmos import CosmosClient

# Replace with your actual endpoint and key
endpoint = "YOUR_COSMOS_DB_ENDPOINT"
key = "YOUR_COSMOS_DB_KEY"
database_id = "YOUR_DATABASE_ID"
container_id = "YOUR_CONTAINER_ID"

client = CosmosClient(endpoint, key)
database = client.get_database_client(database_id)
container = database.get_container_client(container_id)

item_to_create = {
    "id": "exampleItem1",
    "name": "Sample Product",
    "price": 19.99,
    "tags": ["electronics", "gadget"]
}

try:
    created_item = container.create_item(body=item_to_create)
    print(f"Created item with id: {created_item['id']}")
except Exception as e:
    print(f"An error occurred: {e}")
```
                            

Read an Item

Fetch a specific item from the container using its unique ID and partition key (if applicable).

Example Item Read
```python
from azure.cosmos import CosmosClient

# Replace with your actual endpoint and key
endpoint = "YOUR_COSMOS_DB_ENDPOINT"
key = "YOUR_COSMOS_DB_KEY"
database_id = "YOUR_DATABASE_ID"
container_id = "YOUR_CONTAINER_ID"
item_id_to_read = "exampleItem1" # The ID of the item to fetch
partition_key_value = None # Replace with partition key value if applicable

client = CosmosClient(endpoint, key)
database = client.get_database_client(database_id)
container = database.get_container_client(container_id)

try:
    if partition_key_value:
        item = container.read_item(item=item_id_to_read, partition_key=partition_key_value)
    else:
        item = container.read_item(item=item_id_to_read, partition_key=item_id_to_read) # If ID is also the partition key
    print(f"Read item: {item}")
except Exception as e:
    print(f"An error occurred: {e}")
```
                            

Update an Item

Modify an existing item. You can either replace the entire item or perform a partial update (patch).

Example Item Update (Replace)
```python
from azure.cosmos import CosmosClient

# Replace with your actual endpoint and key
endpoint = "YOUR_COSMOS_DB_ENDPOINT"
key = "YOUR_COSMOS_DB_KEY"
database_id = "YOUR_DATABASE_ID"
container_id = "YOUR_CONTAINER_ID"
item_id_to_update = "exampleItem1"
partition_key_value = None # Replace if needed

client = CosmosClient(endpoint, key)
database = client.get_database_client(database_id)
container = database.get_container_client(container_id)

updated_item_data = {
    "id": item_id_to_update,
    "name": "Updated Sample Product",
    "price": 22.50,
    "stock": 150,
    "tags": ["electronics", "gadget", "sale"]
}

try:
    if partition_key_value:
        replaced_item = container.replace_item(item=item_id_to_update, body=updated_item_data, partition_key=partition_key_value)
    else:
        replaced_item = container.replace_item(item=item_id_to_update, body=updated_item_data, partition_key=item_id_to_update) # If ID is partition key
    print(f"Replaced item with id: {replaced_item['id']}")
except Exception as e:
    print(f"An error occurred: {e}")
```
                            
Example Item Update (Patch)
```python
from azure.cosmos import CosmosClient
from azure.cosmos.exceptions import CosmosHttpResponseError
import json

# Replace with your actual endpoint and key
endpoint = "YOUR_COSMOS_DB_ENDPOINT"
key = "YOUR_COSMOS_DB_KEY"
database_id = "YOUR_DATABASE_ID"
container_id = "YOUR_CONTAINER_ID"
item_id_to_patch = "exampleItem1"
partition_key_value = None # Replace if needed

client = CosmosClient(endpoint, key)
database = client.get_database_client(database_id)
container = database.get_container_client(container_id)

# Example: Increment price and add a new field 'onSale'
patch_operations = [
    {"op": "set", "path": "/price", "value": 25.00},
    {"op": "add", "path": "/onSale", "value": True},
    {"op": "add", "path": "/discountPercentage", "value": 10}
]

try:
    if partition_key_value:
        patched_item = container.patch_item(item=item_id_to_patch, partition_key=partition_key_value, body=patch_operations)
    else:
        patched_item = container.patch_item(item=item_id_to_patch, partition_key=item_id_to_patch, body=patch_operations) # If ID is partition key
    print(f"Patched item with id: {patched_item['id']}")
except CosmosHttpResponseError as e:
    print(f"A Cosmos error occurred: {e.status_code} - {e.message}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
```
                            

Delete an Item

Remove an item from the container using its ID and partition key.

Example Item Deletion
```python
from azure.cosmos import CosmosClient

# Replace with your actual endpoint and key
endpoint = "YOUR_COSMOS_DB_ENDPOINT"
key = "YOUR_COSMOS_DB_KEY"
database_id = "YOUR_DATABASE_ID"
container_id = "YOUR_CONTAINER_ID"
item_id_to_delete = "exampleItem1"
partition_key_value = None # Replace if needed

client = CosmosClient(endpoint, key)
database = client.get_database_client(database_id)
container = database.get_container_client(container_id)

try:
    if partition_key_value:
        container.delete_item(item=item_id_to_delete, partition_key=partition_key_value)
    else:
        container.delete_item(item=item_id_to_delete, partition_key=item_id_to_delete) # If ID is partition key
    print(f"Deleted item with id: {item_id_to_delete}")
except Exception as e:
    print(f"An error occurred: {e}")
```