Update
Jump to the Code block for a combined complete example.
Description
Demonstrates single record updates in an Aerospike database.
Setup
Boilerplate
The following examples leverage the following boilerplate to initialize the Aerospike module and connect to a server before updating a record.
const Aerospike = await import("aerospike");
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Establishes a connection to the serverconst client = await Aerospike.connect(config);
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. The client employs an exists
action, specified within the write policy, to define the operation semantics when a record already exists. The default is IGNORE
.
The following variants are available:
Action Type | Description |
---|---|
IGNORE | Create if record doesn’t exist, update otherwise. |
CREATE | Create if record doesn’t exist already. |
UPDATE | Update only, if record exists. |
REPLACE | Replace only, if record exists. |
CREATE_OR_REPLACE | Create if record doesn’t exist, replace otherwise. |
The following example sets the policy to update the record, only if it already exists.
const updatePolicy = new Aerospike.WritePolicy({ exists: Aerospike.policy.exists.UPDATE,});
Update a record
Update
With the write policy exists
action set to UPDATE
or IGNORE
, 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 valueconst newPosted = { posted: 20220602 };
// Metadata may be passed; use null to send no metadataconst metadata = null;
// Update record with new bin valueawait client.put(key, newPosted, metadata, updatePolicy);
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.
// Update bin with value to be addedconst newPosted = { posted: -1 };
// Metadata may be passed; use null to send no metadataconst metadata = null;
// Update record with new bin valueawait client.incr(key, newPosted, metadata, updatePolicy);
Replace
To replace a record, set the exists
action in the write policy to either CREATE_OR_REPLACE
or REPLACE
. 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 policyupdatePolicy.exists = Aerospike.policy.exists.REPLACE;
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 nullconst removeBin = { posted: null };
// Metadata may be passed; use null to send no metadataconst metadata = null;
// Update record with new bin valueawait client.put(key, removeBin, metadata, updatePolicy);
Code block
Expand this section for a single code block to read a record
const Aerospike = await import("aerospike");
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Establishes a connection to the serverconst client = await Aerospike.connect(config);
const updatePolicy = new Aerospike.WritePolicy({ exists: Aerospike.policy.exists.UPDATE,});
// Create bin with new valueconst newPosted = { posted: 20220602 };
// Metadata may be passed; use null to send no metadataconst metadata = null;
// Update record with new bin valueawait client.put(key, newPosted, metadata, updatePolicy);
// Close the connection to the serverawait client.close();