# Update

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

## Setup

The following examples will use the setup and record below to illustrate single record updates in an Aerospike database.

```csharp
using Aerospike.Client;

// Define host configuration

Host config = new Host("127.0.0.1", 3000);

// Establishes a connection to the server

AerospikeClient client = new AerospikeClient(null, config);

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001

Key key = new Key("sandbox", "ufodata", 5001);
```

The record structure:

```plaintext
+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+

| PK   | occurred | reported | posted   | report                                                                                                                                                                                                                 | location                                                   |

+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+

| 5001 | 20220531 | 20220601 | 20220601 | MAP('{"shape":["circle", "flash", "disc"], "summary":"Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!", "city":"Ann Arbor", "state":"Michigan", "duration":"5 minutes"}') | GeoJSON('{"type":"Point","coordinates":[42.2808,83.743]}') |

+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
```

## Policies

Create and Update both utilize write [policies](https://aerospike.com/docs/database/learn/policies) that can be defined per command. The client employs a `RecordExistsAction`, specified within the write policy, to define the operation semantics when a record already exists. The default is `UPDATE`.

The following variants are available:

| Action Type | Description |
| --- | --- |
| `UPDATE` | Create if record doesn’t exist, update otherwise. |
| `UPDATE_ONLY` | Update only, if record exists. |
| `REPLACE` | Create if record doesn’t exist, replace otherwise. |
| `REPLACE_ONLY` | Replace only, if record exists. |
| `CREATE_ONLY` | Create if record doesn’t exist already. |

The following example sets the policy to update the record, only if it already exists.

```csharp
// Can use a previously defined write policy or create a new one

WritePolicy updatePolicy = new WritePolicy();

updatePolicy.recordExistsAction = RecordExistsAction.UPDATE_ONLY;
```

## Update a record

### Update

With the write policy `RecordExistsAction` set to `UPDATE` or `UPDATE_ONLY`, new bins can be added to a record and existing bin values can be updated.

The following example updates the `posted` bin value to `20220602`.

```csharp
// Create bin with new value

Bin newPosted = new Bin("posted", 20220602);

// Update record with new bin value

client.Put(updatePolicy, key, newPosted);

// Close the connection to the server

client.Close();
```

### Type specific

Type specific updates can `add` integers or `append`/`prepend` strings to existing integer and string bin values, respectively.

The following example decrements the `posted` bin value by one.

```csharp
// Create bin with value to be added

Bin newPosted = new Bin("posted", -1);

// Add new bin value to existing bin

client.Add(updatePolicy, key, newPosted);

// Close the connection to the server

client.Close();
```

::: note
Best practice is to apply `add` and `append`/`prepend` operations as part of a command, and not to use the full-record operation variant described above. Refer to [Transaction Operations](https://aerospike.com/docs/develop/client/csharp/usage/atomic/multi) for information on operations and more client examples.
:::

### Replace

To replace a record, set the `RecordExistsAction` in the write policy to either `REPLACE` or `REPLACE_ONLY`. Refer to the [policies](#policies) section above for more information on setting the policy and the [create](https://aerospike.com/docs/develop/client/csharp/usage/atomic/create) page for creating a record to replace the existing one.

The following example sets the existing write policy to replace ony if the record already exists.

```csharp
// Updating the previously defined write policy

updatePolicy.recordExistsAction = RecordExistsAction.REPLACE_ONLY;
```

::: note
A replace can have a performance advantage over an update as a replace may not require reading the record from the device. Certain situations such as a stored key or a secondary index on a bin, and more, may still require a read on a replace, eliminating any performance advantage.
:::

### Delete a bin

A bin is deleted by setting it to `null`. Delete the `posted` bin by setting its value to `null`.

```csharp
// Set bin value to null

Bin removeBin = Bin.AsNull("posted");

// Update record with null bin

client.Put(null, key, removeBin);

// Close the connection to the server

client.Close();
```

::: note
Updating a bin value to `null`, when it is the only bin in the record, deletes the record.
:::

## Code block

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

```csharp
using Aerospike.Client;

// Define host configuration

Host config = new Host("127.0.0.1", 3000);

// Establishes a connection to the server

AerospikeClient client = new AerospikeClient(null, config);

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001

Key key = new Key("sandbox", "ufodata", 5001);

// Can use a previously defined write policy or create a new one

WritePolicy updatePolicy = new WritePolicy();

updatePolicy.recordExistsAction = RecordExistsAction.UPDATE_ONLY;

Bin newPosted = new Bin("posted", 20220602);

// Update record with new bin value

client.Put(updatePolicy, key, newPosted);

// Close the connection to the server

client.Close();
```