ArangoDB

Hash Indexes

This is an introduction to ArangoDB's hash indexes.



Hash Indexes

It is possible to define a hash index on one or more attributes (or paths) of a documents. This hash is then used in queries to locate documents in O(1) operations. If the hash is unique, then no two documents are allowed to have the same set of attribute values.

Accessing Hash Indexes from the Shell


ensureUniqueConstraint(field1, field2, ..., fieldn)
Creates a unique hash index on all documents using field1, field2, ... as attribute paths. At least one attribute path must be given.

When a unique constraint is in effect for a collection, then all documents which contain the given attributes must differ in the attribute values. Creating a new document or updating a document will fail, if the uniqueness is violated. If any attribute value is null for a document, this document is ignored by the index.

Note that non-existing attribute paths in a document are treat as if the value were null.

In case that the index was successfully created, the index identifier is returned.

Examples

arango> db.four.ensureUniqueConstraint("a", "b.c");
{ "id" : "164405/1147445", "unique" : true, "type" : "hash", "fields" : ["a", "b.c"], "isNewlyCreated" : true }

arango> db.four.save({ a : 1, b : { c : 1 } });
{ "_id" : "164405/1868341", "_rev" : 1868341 }

arango> db.four.save({ a : 1, b : { c : 1 } });
JavaScript exception in file '(arango)' at 1,9: [ArangoError 1210: cannot save document]
!db.four.save({ a : 1, b : { c : 1 } });
!        ^
stacktrace: [ArangoError 1210: cannot save document]
    at (arango):1:9

arango> db.four.save({ a : 1, b : { c : null } });
{ "_id" : "164405/2196021", "_rev" : 2196021 }

arango> db.four.save({ a : 1 });
{ "_id" : "164405/2196023", "_rev" : 2196023 }


ensureHashIndex(field1, field2, ..., fieldn)
Creates a unique hash index on all documents using field1, field2, ... as attribute paths. At least one attribute path must be given.

Note that non-existing attribute paths in a document are treat as if the value were null.

In case that the index was successfully created, the index identifier is returned.

Examples

arango> db.test.ensureHashIndex("a");
{ "id" : "4873815/5922391", "unique" : false, "type" : "hash", "fields" : ["a"], "isNewlyCreated" : true }

arango> db.test.save({ a : 1 });
{ "_id" : "4873815/6381143", "_rev" : 6381143 }

arango> db.test.save({ a : 1 });
{ "_id" : "4873815/6446679", "_rev" : 6446679 }

arango> db.test.save({ a : null });
{ "_id" : "4873815/6708823", "_rev" : 6708823 }