Skip to content
Visit booth 3171 at Google Cloud Next to see how to unlock real-time decisions at scaleMore info

Update

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.

import (
"fmt"
"github.com/aerospike/aerospike-client-go/v6"
)
// Establishes a connection to the server
client, err := aerospike.NewClient("127.0.0.1", 3000)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
key, err := aerospike.NewKey("sandbox", "ufodata", 5001)
if err != nil {
log.Fatal(err)
}

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 command. 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
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.
CREATE_ONLYCreate if record doesn’t exist already.

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

// Can use a previously defined write policy or create a new one
updatePolicy := aerospike.NewWritePolicy(0,0)
updatePolicy.RecordExistsAction = aerospike.UPDATE_ONLY

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.

// Create bin with new value
newPosted := aerospike.NewBin("posted", 20220602)
// Update record with new bin value
err = client.PutBins(updatePolicy, key, newPosted)
if err != nil {
log.Fatal(err)
}

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.

// Create bin with value to be added
newPosted := aerospike.NewBin("posted", -1)
// Add new bin value to existing bin
err = client.AddBins(updatePolicy, key, newPosted)
if err != nil {
log.Fatal(err)
}

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.

// Updating the previously defined write policy
updatePolicy.RecordExistsAction = aerospike.REPLACE_ONLY

Delete a bin

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

// Set bin value to null
removeBin := aerospike.NewBin("posted", nil)
// Update record with null bin
err = client.PutBins(nil, key, removeBin)
if err != nil {
log.Fatal(err)
}

Code block

Expand this section for a single code block to read a record
import (
"fmt"
"github.com/aerospike/aerospike-client-go/v6"
)
func main() {
// Establishes a connection to the server
client, err := aerospike.NewClient("127.0.0.1", 3000)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
key, err := aerospike.NewKey("sandbox", "ufodata", 5001)
if err != nil {
log.Fatal(err)
}
// Can use a previously defined write policy or create a new one
updatePolicy := aerospike.NewWritePolicy(0,0)
updatePolicy.RecordExistsAction = aerospike.UPDATE_ONLY
// Create bin with new value
newPosted := aerospike.NewBin("posted", 20220602)
// Update record with new bin value
err = client.PutBins(updatePolicy, key, newPosted)
if err != nil {
log.Fatal(err)
}
}
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?