Skip to content

Create

Jump to the Code block for a combined complete example.

Create a document record

You should plan and schedule index creation/removal on production systems as described in Secondary Index Capacity Planning. An index consumes RAM for every index entry. Background index creation/removal can take a substantial amount of resources. It is important to carefully plan and schedule index creation/removal on production systems.

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 = await import("aerospike");
const exp = Aerospike.exp;
// Set hosts to your server's address and port
const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the server
const client = await Aerospike.connect(config);

Create a JSON document

Prepare the JSON document to be sent to Aerospike.

const data = JSON.parse(await readFile("./employees.json", "utf8"));
const bins = { employees: data.employees };
const metadata = null;

Source document:

{
"employees": [
{
"id": "09",
"name": "Nitin",
"department": "Finance"
},
{
"id": "10",
"name": "Jonathan",
"department": "Human Resources"
},
{
"id": "11",
"name": "Caitlin",
"department": "Engineering"
}
]
}

Write

Write the document to Aerospike.

await client.put(key, bins, metadata, writePolicy);

Code block

Expand this section for a single code block to create a document record.
import { readFile } from "node:fs/promises";
const Aerospike = await import("aerospike");
// Set hosts to your server's address and port
const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Create a key
const key = new Aerospike.Key("sandbox", "ufodata", 5001);
// Create a new write policy
const writePolicy = new Aerospike.WritePolicy({
key: Aerospike.policy.key.SEND,
});
// Establishes a connection to the server
const client = await Aerospike.connect(config);
const data = JSON.parse(await readFile("./employees.json", "utf8"));
const bins = { employees: data.employees };
// Metadata may be passed; use null to send no metadata
const metadata = null;
await client.put(key, bins, metadata, writePolicy);
await client.close();
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?