Skip to the content.

Manipulating indexes

These functions implement the HTTP API for manipulating indexes.

collection.createIndex

async collection.createIndex(details): Object

Creates an arbitrary index on the collection.

Arguments

Examples

const db = new Database();
const collection = db.collection("some-collection");
const index = await collection.createIndex({
  type: "hash",
  fields: ["a", "a.b"],
});
// the index has been created with the handle `index.id`

collection.createHashIndex

async collection.createHashIndex(fields, [opts]): Object

Creates a hash index on the collection.

Arguments

For more information on hash indexes, see the HTTP API for hash indexes.

Examples

const db = new Database();
const collection = db.collection("some-collection");

const index = await collection.createHashIndex("favorite-color");
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["favorite-color"]);

// -- or --

const index = await collection.createHashIndex(["favorite-color"]);
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["favorite-color"]);

collection.createSkipList

async collection.createSkipList(fields, [opts]): Object

Creates a skiplist index on the collection.

Arguments

For more information on skiplist indexes, see the HTTP API for skiplist indexes.

Examples

const db = new Database();
const collection = db.collection("some-collection");

const index = await collection.createSkipList("favorite-color");
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["favorite-color"]);

// -- or --

const index = await collection.createSkipList(["favorite-color"]);
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["favorite-color"]);

collection.createGeoIndex

async collection.createGeoIndex(fields, [opts]): Object

Creates a geo-spatial index on the collection.

Arguments

For more information on the properties of the opts object see the HTTP API for manipulating geo indexes.

Examples

const db = new Database();
const collection = db.collection("some-collection");

const index = await collection.createGeoIndex(["latitude", "longitude"]);
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["longitude", "latitude"]);

// -- or --

const index = await collection.createGeoIndex("location", { geoJson: true });
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["location"]);

collection.createFulltextIndex

async collection.createFulltextIndex(fields, [minLength]): Object

Creates a fulltext index on the collection.

Arguments

For more information on fulltext indexes, see the HTTP API for fulltext indexes.

Examples

const db = new Database();
const collection = db.collection("some-collection");

const index = await collection.createFulltextIndex("description");
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["description"]);

// -- or --

const index = await collection.createFulltextIndex(["description"]);
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["description"]);

collection.createPersistentIndex

async collection.createPersistentIndex(fields, [opts]): Object

Creates a Persistent index on the collection. Persistent indexes are similarly in operation to skiplist indexes, only that these indexes are in disk as opposed to in memory. This reduces memory usage and DB startup time, with the trade-off being that it will always be orders of magnitude slower than in-memory indexes.

Arguments

For more information on the properties of the opts object see the HTTP API for manipulating Persistent indexes.

Examples

const db = new Database();
const collection = db.collection("some-collection");

const index = await collection.createPersistentIndex(["name", "email"]);
// the index has been created with the handle `index.id`
assert.deepEqual(index.fields, ["name", "email"]);

collection.index

async collection.index(indexHandle): Object

Fetches information about the index with the given indexHandle and returns it.

Arguments

Examples

const db = new Database();
const collection = db.collection("some-collection");
const index = await collection.createFulltextIndex("description");
const result = await collection.index(index.id);
assert.equal(result.id, index.id);
// result contains the properties of the index

// -- or --

const result = await collection.index(index.id.split("/")[1]);
assert.equal(result.id, index.id);
// result contains the properties of the index

collection.indexes

async collection.indexes(): Array<Object>

Fetches a list of all indexes on this collection.

Examples

const db = new Database();
const collection = db.collection("some-collection");
await collection.createFulltextIndex("description");
const indexes = await collection.indexes();
assert.equal(indexes.length, 1);
// indexes contains information about the index

collection.dropIndex

async collection.dropIndex(indexHandle): Object

Deletes the index with the given indexHandle from the collection.

Arguments

Examples

const db = new Database();
const collection = db.collection("some-collection");
const index = await collection.createFulltextIndex("description");
await collection.dropIndex(index.id);
// the index has been removed from the collection

// -- or --

await collection.dropIndex(index.id.split("/")[1]);
// the index has been removed from the collection

collection.createCapConstraint

async collection.createCapConstraint(size): Object

Creates a cap constraint index on the collection.

Warning: This method is not available when targeting ArangoDB 3.0 or later, see Compatibility.

Arguments

If size is a number, it will be interpreted as size.size.

For more information on the properties of the size object see the HTTP API for creating cap constraints.

Examples

const db = new Database();
const collection = db.collection("some-collection");

const index = await collection.createCapConstraint(20);
// the index has been created with the handle `index.id`
assert.equal(index.size, 20);

// -- or --

const index = await collection.createCapConstraint({ size: 20 });
// the index has been created with the handle `index.id`
assert.equal(index.size, 20);