Read
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.
const Aerospike = require('aerospike');const maps = Aerospike.maps
// Define host configurationlet config = {hosts: '127.0.0.1:3000'};
// Create a new write policylet writePolicy = new Aerospike.WritePolicy({ key: Aerospike.policy.key.SEND});
// Create the record keylet key = new Aerospike.Key('test', 'table1', 5);
Prepare the JSON document to be sent to Aerospike.
let employees = [ {"id": "09", "name": "Nitin", "department": "Finance"}, {"id": "10", "name": "Jonathan", "department": "Human Resources"}, {"id": "11", "name": "Caitlin", "department": "Engineering"}]
console.info("Employees record:\\n", employees);
Write
Write the document to Aerospike.
let bins = { employees };
// Write the record to Aerospike;(async () => { // Establishes a connection to the server let client = await Aerospike.connect(config); console.info("Connected to server\\n");
await client.put(key, bins, [], writePolicy); console.info("Wrote record to database\\n");
Read
Read the name
field from the third record in the map list.
// Get 'name' field from 3rd employee record const ops = [ maps.getByKey('employees', 'name') .withContext((ctx) => ctx.addListIndex(2)) .andReturn(maps.returnType.VALUE) ] const result = await client.operate(key, ops) console.info('Name of 3rd employee: ', result.bins.employees)
// Close the connection to the server client.close();})();
Code block
Expand this section for a single code block to read a document record.
const Aerospike = require('aerospike');const maps = Aerospike.maps
// Define host configurationlet config = {hosts: '127.0.0.1:3000'};
// Create a new write policylet writePolicy = new Aerospike.WritePolicy({ key: Aerospike.policy.key.SEND});
// Create the record keylet key = new Aerospike.Key('test', 'table1', 5);
let employees = [ {"id": "09", "name": "Nitin", "department": "Finance"}, {"id": "10", "name": "Jonathan", "department": "Human Resources"}, {"id": "11", "name": "Caitlin", "department": "Engineering"}]
console.info("Employees record:\\n", employees);
let bins = { employees };
// Write the record to Aerospike;(async () => { // Establishes a connection to the server let client = await Aerospike.connect(config); console.info("Connected to server\\n");
await client.put(key, bins, [], writePolicy); console.info("Wrote record to database\\n");
// Get 'name' field from 3rd employee record const ops = [ maps.getByKey('employees', 'name') .withContext((ctx) => ctx.addListIndex(2)) .andReturn(maps.returnType.VALUE) ] const result = await client.operate(key, ops) console.info('Name of 3rd employee: ', result.bins.employees)
// Close the connection to the server client.close();})();