Create
Jump to the Code block for a combined complete example.
Create a document record
The following command uses the Aerospike Admin (asadm) to create an integer index on the sandbox
namespace, ufodata
set, and occurred
bin. This is the recommended way to create a secondary index.
asadm -e 'enable; manage sindex create numeric occurred_idx ns sandbox set ufodata bin occurred'
Setup
Import the necessary helpers, create a client connection, and create a key.
const Aerospike = require('aerospike');
// 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);
Create a JSON document
Prepare the JSON document to be sent to Aerospike.
let employee = {id:"09", name: "Nitin", department:"Finance"}
console.info("Employee record:\\n", employee);
Write
Write the document to Aerospike.
let bins = { employee };
// 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");
// Close the connection to the server client.close();})();
Code block
Expand this section for a single code block to create a document record.
const Aerospike = require('aerospike');
// 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 employee = {id:"09", name: "Nitin", department:"Finance"}
console.info("Employee record:\\n", employee);
let bins = { employee };
// 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");
// Close the connection to the server client.close();})();