ArangoDB v2.8 reached End of Life (EOL) and is no longer supported.
This documentation is outdated. Please see the most recent version here: Try latest
Hash Indexes
Introduction to Hash Indexes
This is an introduction to ArangoDB’s hash indexes.
It is possible to define a hash index on one or more attributes (or paths) of a document. This hash index is then used in queries to locate documents in O(1) operations. If the hash index is unique, then no two documents are allowed to have the same set of attribute values.
Creating a new document or updating a document will fail if the uniqueness is violated.
If the index is declared sparse, a document will be excluded from the index and no
uniqueness checks will be performed if any index attribute value is not set or has a value
of null
.
Accessing Hash Indexes from the Shell
collection.ensureIndex({ type: "hash", fields: [ "field1", ..., "fieldn" ], unique: true })
Creates a unique hash index on all documents using field1, … fieldn
as attribute paths. At least one attribute path has to be given.
The index will be non-sparse by default.
All documents in the collection must differ in terms of the indexed
attributes. Creating a new document or updating an existing document will
will fail if the attribute uniqueness is violated.
To create a sparse unique index, set the sparse attribute to true
:
collection.ensureIndex({ type: "hash", fields: [ "field1", ..., "fieldn" ], unique: true, sparse: true })
In case that the index was successfully created, the index identifier is returned.
Non-existing attributes will default to null
.
In a sparse index all documents will be excluded from the index for which all
specified index attributes are null
. Such documents will not be taken into account
for uniqueness checks.
In a non-sparse index, all documents regardless of null
- attributes will be
indexed and will be taken into account for uniqueness checks.
In case that the index was successfully created, an object with the index
details, including the index-identifier, is returned.
arangosh> db.test.ensureIndex({ type: "hash", fields: [ "a", "b.c" ], unique: true });
{
"id" : "test/1136937089",
"type" : "hash",
"fields" : [
"a",
"b.c"
],
"selectivityEstimate" : 1,
"unique" : true,
"sparse" : false,
"isNewlyCreated" : true,
"code" : 201
}
arangosh> db.test.save({ a : 1, b : { c : 1 } });
{
"_id" : "test/1137264769",
"_rev" : "1137264769",
"_key" : "1137264769"
}
arangosh> db.test.save({ a : 1, b : { c : 1 } });
[ArangoError 1210: cannot create document, unique constraint violated]
arangosh> db.test.save({ a : 1, b : { c : null } });
{
"_id" : "test/1137723521",
"_rev" : "1137723521",
"_key" : "1137723521"
}
arangosh> db.test.save({ a : 1 });
[ArangoError 1210: cannot create document, unique constraint violated]
arangosh> db.test.ensureIndex({ type: "hash", fields: [ "a", "b.c" ], unique: true });
arangosh> db.test.save({ a : 1, b : { c : 1 } });
arangosh> db.test.save({ a : 1, b : { c : 1 } });
arangosh> db.test.save({ a : 1, b : { c : null } });
arangosh> db.test.save({ a : 1 });
collection.ensureIndex({ type: "hash", fields: [ "field1", ..., "fieldn" ] })
Creates a non-unique hash index on all documents using field1, … fieldn
as attribute paths. At least one attribute path has to be given.
The index will be non-sparse by default.
To create a sparse unique index, set the sparse attribute to true
:
collection.ensureIndex({ type: "hash", fields: [ "field1", ..., "fieldn" ], sparse: true })
In case that the index was successfully created, an object with the index
details, including the index-identifier, is returned.
arangosh> db.test.ensureIndex({ type: "hash", fields: [ "a" ] });
{
"id" : "test/1134119041",
"type" : "hash",
"fields" : [
"a"
],
"selectivityEstimate" : 1,
"unique" : false,
"sparse" : false,
"isNewlyCreated" : true,
"code" : 201
}
arangosh> db.test.save({ a : 1 });
{
"_id" : "test/1134381185",
"_rev" : "1134381185",
"_key" : "1134381185"
}
arangosh> db.test.save({ a : 1 });
{
"_id" : "test/1134577793",
"_rev" : "1134577793",
"_key" : "1134577793"
}
arangosh> db.test.save({ a : null });
{
"_id" : "test/1134839937",
"_rev" : "1134839937",
"_key" : "1134839937"
}
arangosh> db.test.ensureIndex({ type: "hash", fields: [ "a" ] });
arangosh> db.test.save({ a : 1 });
arangosh> db.test.save({ a : 1 });
arangosh> db.test.save({ a : null });