Update
For the complete documentation index see: llms.txt
All documentation pages available in markdown.
Jump to the 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). A JSON object is equivalent to a map, and a JSON array is equivalent to a list.
In this example, we create the following JSON document:
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. 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.
using Aerospike.Client;using System;using System.Collections.Generic;
AerospikeClient client = new("localhost", 3000);
string ns = "test";string setName = "table1";int keyId = 6;
Key key = new(ns, setName, keyId);Prepare the JSON document to be sent to Aerospike.
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.
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);}Update
Update the newly created document.
try{ client.Operate(null, key, MapOperation.Put( MapPolicy.Default, "employees", Value.Get("department"), Value.Get("Support"), CTX.ListIndex(2)) );
Record updated = client.Get(null, key); Console.WriteLine("Updated record successfully:\n{0}", updated.GetValue("employees"));}catch (AerospikeException e){ Console.WriteLine("Update failed\nError: {0}", e.Message);}Read
Read the newly updated document.
Record record = client.Operate(null, key, CDTOperation.SelectByPath( "employees", SelectFlag.VALUE, CTX.ListIndex(2), CTX.MapKey(Value.Get("department"))));
Console.WriteLine("Element 2's value in the 'department' field: {0}", record.GetValue("employees"));
client.Close();Code block
Expand this section for a single code block to update a document record.
using Aerospike.Client;using System;using System.Collections.Generic;
AerospikeClient client = new("localhost", 3000);
string ns = "test";string setName = "table1";int keyId = 6;
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);}
try{ client.Operate(null, key, MapOperation.Put( MapPolicy.Default, "employees", Value.Get("department"), Value.Get("Support"), CTX.ListIndex(2)) );
Record updated = client.Get(null, key); Console.WriteLine("Updated record successfully:\n{0}", updated.GetValue("employees"));}catch (AerospikeException e){ Console.WriteLine("Update failed\nError: {0}", e.Message);}
Record record = client.Operate(null, key, CDTOperation.SelectByPath( "employees", SelectFlag.VALUE, CTX.ListIndex(2), CTX.MapKey(Value.Get("department"))));
Console.WriteLine("Element 2's value in the 'department' field: {0}", record.GetValue("employees"));
client.Close();