Skip to main content

Update a Record

Jump to the 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.

The record structure:

+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
| 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 that can be defined per transaction. Each client employs an exists action, 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 TypeDescription
CREATE ONLYCreate if record doesn't exist already.
UPDATECreate if record doesn't exist, update otherwise.
UPDATE ONLYUpdate only, if record exists.
REPLACECreate if record doesn't exist, replace otherwise.
REPLACE ONLYReplace only, if record exists.
note

The action type naming varies by client. Refer to the client specific API reference for more information.

Java |  C |  Go |  C# |  Python |  Node.js |  Ruby |  Rust

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

Update a record

Update

With the write policy exists action 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.

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.

note

Best practice is to apply add and append/prepend operations as part of a transaction, and not to use the full-record operation variant described above. Refer to Transaction Operations for information on operations and more client examples.

Replace

To replace a record, set the exists action in the write policy to either REPLACE or REPLACE ONLY. Refer to the policies section above for more information on setting the policy and the 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.

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.

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