---
title: "Update"
description: "Update Aerospike JSON documents using the Python client with CDT list and map operations."
---

# Update

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

Jump to the [Code block](#code-block) for a combined complete example.

## Create a document record

The following example demonstrates reading a document record with a JSON helper library. In Aerospike, JSON documents are handled as a [Collection Data Type (CDT)](https://aerospike.com/docs/develop/data-types/collections). A JSON object is equivalent to a [map](https://aerospike.com/docs/develop/data-types/collections/map), and a JSON array is equivalent to a [list](https://aerospike.com/docs/develop/data-types/collections/list).

In this example, we create the following JSON document:

```json
employees = [{"id": "09", "name": "Nitin", "department": "Finance"},

             {"id": "10", "name": "Jonathan", "department": "Human Resources"},

             {"id": "11", "name": "Caitlin", "department": "Engineering"}]
```

The JSON document is added to a bin called `employees` which is of data type [list](https://aerospike.com/docs/develop/data-types/collections/list). There are some advantages to holding an entire document in a single bin, rather than spreading out the document’s fields over multiple bins. There is less metadata overhead when namespaces contain fewer, larger bins.

### Setup

Import the necessary helpers, create a client connection, and create a key.

```python
import aerospike

from aerospike_helpers.operations import map_operations

from aerospike_helpers import cdt_ctx

import pprint

import json

# set aerospike host config

config = {"hosts": [("localhost", 3000)]}

# create the aerospike client and connect

client = aerospike.client(config)

# Aerospike namespace, set, and key_id to be used for the Aerospike key

namespace = "test"

as_set = "table1"

key_id = 6

# define Aerospike key

key = (namespace, as_set, key_id)
```

Prepare the JSON document to be sent to Aerospike.

```python
# example JSON string

employees = """[{"id": "09", "name": "Nitin", "department": "Finance"},

                {"id": "10", "name": "Jonathan", "department": "Human Resources"},

                {"id": "11", "name": "Caitlin", "department": "Engineering"}]"""

# Convert string to Python dict

employee_dict = json.loads(employees)

pprint.pprint(employee_dict)

# Set an Aerospike bin with the bin name "employees" and put

# the JSON document in as a list.

bins = {"employees": employee_dict}
```

### Write

Write the document to Aerospike.

```python
# Create the write policy

write_policy = {"key": aerospike.POLICY_KEY_SEND}

# Write the record to Aerospike

try:

    client.put(key=key, bins=bins, policy=write_policy)

    (key_, meta, bins) = client.get(key=key)

    print("Create succeeded\nKey: ", key[2], "\nRecord:")

    pprint.pprint(bins)

except aerospike.exception.AerospikeError as e:

    print(f"Create failed\nError:{e.msg},{e.code}")
```

### Update

Update the newly created document.

```python
# Create the update request. Update the employee at index[2]'s

# department from "Engineering" to "Support".

ctx = [cdt_ctx.cdt_ctx_list_index(2)]

ops = [

    map_operations.map_put(bin_name="employees", key="department", value="Support", ctx=ctx)

]

try:

    ret_key, ret_meta, ret_bins = client.operate(key=key, list=ops)

except aerospike.exception.AerospikeError as e:

    print(f"Update failed\nError:{e.msg},{e.code}")

else:

    print("Updated record successfully:")

    pprint.pprint(ret_bins)
```

### Read

Read the newly updated document.

```python
# read the 'department' value for from the employees map list's last map

ctx = [cdt_ctx.cdt_ctx_list_index(2)]

ops = [

    map_operations.map_get_by_key(bin_name="employees", key="department", return_type=aerospike.MAP_RETURN_VALUE,

                                  ctx=ctx)

]

ret_key, ret_meta, ret_bins = client.operate(key=key, list=ops)

print("element 2's value in their 'department' field: ")

pprint.pprint(ret_bins)
```

## Code block

Expand this section for a single code block to update a document record.

```python
import aerospike

from aerospike_helpers.operations import map_operations

from aerospike_helpers import cdt_ctx

import pprint

import json

# set aerospike host config

config = {"hosts": [("localhost", 3000)]}

# create the aerospike client and connect

client = aerospike.client(config)

# Aerospike namespace, set, and key_id to be used for the Aerospike key

namespace = "test"

as_set = "table1"

key_id = 6

# define Aerospike key

key = (namespace, as_set, key_id)

# example JSON string

employees = """[{"id": "09", "name": "Nitin", "department": "Finance"},

                {"id": "10", "name": "Jonathan", "department": "Human Resources"},

                {"id": "11", "name": "Caitlin", "department": "Engineering"}]"""

# Convert string to Python dict

employee_dict = json.loads(employees)

pprint.pprint(employee_dict)

# Set an Aerospike bin with the bin name "employees" and put

# the JSON document in as a list.

bins = {"employees": employee_dict}

# Create the write policy

write_policy = {"key": aerospike.POLICY_KEY_SEND}

# Write the record to Aerospike

try:

    client.put(key=key, bins=bins, policy=write_policy)

    (key_, meta, bins) = client.get(key=key)

    print("Create succeeded\nKey: ", key[2], "\nRecord:")

    pprint.pprint(bins)

except aerospike.exception.AerospikeError as e:

    print(f"Create failed\nError:{e.msg},{e.code}")

# Create the update request. Update the employee at index[2]'s

# department from "Engineering" to "Support".

ctx = [cdt_ctx.cdt_ctx_list_index(2)]

ops = [

    map_operations.map_put(bin_name="employees", key="department", value="Support", ctx=ctx)

]

try:

    ret_key, ret_meta, ret_bins = client.operate(key=key, list=ops)

except aerospike.exception.AerospikeError as e:

    print(f"Update failed\nError:{e.msg},{e.code}")

else:

    print("Updated record successfully:")

    pprint.pprint(ret_bins)

# read the 'department' value for from the employees map list's last map

ctx = [cdt_ctx.cdt_ctx_list_index(2)]

ops = [

    map_operations.map_get_by_key(bin_name="employees", key="department", return_type=aerospike.MAP_RETURN_VALUE,

                                  ctx=ctx)

]

ret_key, ret_meta, ret_bins = client.operate(key=key, list=ops)

print("element 2's value in their 'department' field: ")

pprint.pprint(ret_bins)
```