---
title: "Read"
description: "Learn how to read JSON-like document records and Collection Data Types (CDT) using the Aerospike C# client."
---

# Read

> 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.

```csharp
using Aerospike.Client;

using System;

using System.Collections.Generic;

AerospikeClient client = new("localhost", 3000);

string ns = "test";

string setName = "table1";

int keyId = 5;

Key key = new(ns, setName, keyId);
```

Prepare the JSON document to be sent to Aerospike.

```csharp
List<Dictionary<string, object>> employees = new()

{

    new() { ["id"] = "09", ["name"] = "Nitin", ["department"] = "Finance" },

    new() { ["id"] = "10", ["name"] = "Jonathan", ["department"] = "Human Resources" },

    new() { ["id"] = "11", ["name"] = "Caitlin", ["department"] = "Engineering" }

};

Bin bin = new("employees", employees);
```

### Write

Write the document to Aerospike.

```csharp
WritePolicy writePolicy = new()

{

    sendKey = true

};

try

{

    client.Put(writePolicy, key, bin);

    Record record = client.Get(null, key);

    Console.WriteLine("Create succeeded\nKey: {0}\nRecord: {1}",

        key.userKey, record.GetValue("employees"));

}

catch (AerospikeException e)

{

    Console.WriteLine("Create failed\nError: {0}", e.Message);

}
```

### Read

Read the `name` field from the third employee in the list (index 2).

```csharp
Record record = client.Operate(null, key,

    CDTOperation.SelectByPath(

        "employees",

        SelectFlag.VALUE,

        CTX.ListIndex(2),

        CTX.MapKey(Value.Get("name")))

);

Console.WriteLine("The 'name' field at list index 2: {0}",

    record.GetValue("employees"));

client.Close();
```

## Code block

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

```csharp
using Aerospike.Client;

using System;

using System.Collections.Generic;

AerospikeClient client = new("localhost", 3000);

string ns = "test";

string setName = "table1";

int keyId = 5;

Key key = new(ns, setName, keyId);

List<Dictionary<string, object>> employees = new()

{

    new() { ["id"] = "09", ["name"] = "Nitin", ["department"] = "Finance" },

    new() { ["id"] = "10", ["name"] = "Jonathan", ["department"] = "Human Resources" },

    new() { ["id"] = "11", ["name"] = "Caitlin", ["department"] = "Engineering" }

};

Bin bin = new("employees", employees);

WritePolicy writePolicy = new()

{

    sendKey = true

};

try

{

    client.Put(writePolicy, key, bin);

    Record created = client.Get(null, key);

    Console.WriteLine("Create succeeded\nKey: {0}\nRecord: {1}",

        key.userKey, created.GetValue("employees"));

}

catch (AerospikeException e)

{

    Console.WriteLine("Create failed\nError: {0}", e.Message);

}

Record record = client.Operate(null, key,

    CDTOperation.SelectByPath(

        "employees",

        SelectFlag.VALUE,

        CTX.ListIndex(2),

        CTX.MapKey(Value.Get("name")))

);

Console.WriteLine("The 'name' field at list index 2: {0}",

    record.GetValue("employees"));

client.Close();
```