ArangoDB v3.4 reached End of Life (EOL) and is no longer supported.

This documentation is outdated. Please see the most recent version here: Latest Docs

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

  • fields: Array<string>

    An array of names of document fields on which to create the index. If the value is a string, it will be wrapped in an array automatically.

  • opts: Object (optional)

    Additional options for this index. If the value is a boolean, it will be interpreted as opts.unique.

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

  • fields: Array<string>

    An array of names of document fields on which to create the index. If the value is a string, it will be wrapped in an array automatically.

  • opts: Object (optional)

    Additional options for this index. If the value is a boolean, it will be interpreted as opts.unique.

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

  • fields: Array<string>

    An array of names of document fields on which to create the index. Currently, geo indexes must cover exactly one field. If the value is a string, it will be wrapped in an array automatically.

  • opts: Object (optional)

    An object containing additional properties of the index.

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

  • fields: Array<string>

    An array of names of document fields on which to create the index. Currently, fulltext indexes must cover exactly one field. If the value is a string, it will be wrapped in an array automatically.

  • minLength (optional):

    Minimum character length of words to index. Uses a server-specific default value if not specified.

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

  • fields: Array<string>

    An array of names of document fields on which to create the index.

  • opts: Object (optional)

    An object containing additional properties of the index.

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

  • indexHandle: string

    The handle of the index to look up. This can either be a fully-qualified identifier or the collection-specific key of the index. If the value is an object, its id property will be used instead.

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

  • indexHandle: string

    The handle of the index to delete. This can either be a fully-qualified identifier or the collection-specific key of the index. If the value is an object, its id property will be used instead.

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.

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

Arguments

  • size: Object

    An object with any of the following properties:

    • size: number (optional)

      The maximum number of documents in the collection.

    • byteSize: number (optional)

      The maximum size of active document data in the collection (in bytes).

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

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);