Hash Indexes
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 .
Ensures that a unique constraint exists: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 willwill 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 allspecified index attributes are null
. Such documents will not be taken into accountfor uniqueness checks.
In a non-sparse index, all documents regardless of null
- attributes will beindexed and will be taken into account for uniqueness checks.
Show execution results
- {
- "deduplicate" : true,
- "fields" : [
- "a",
- "b.c"
- ],
- "id" : "test/16330",
- "isNewlyCreated" : true,
- "selectivityEstimate" : 1,
- "sparse" : false,
- "type" : "hash",
- "unique" : true,
- "code" : 201
- }
- {
- "_id" : "test/16333",
- "_key" : "16333",
- "_rev" : "_ZHpYszy--_"
- }
- [ArangoError 1210: unique constraint violated - in index 16330 of type hash over ["a","b.c"]; conflicting key: 16333]
- {
- "_id" : "test/16339",
- "_rev" : "_ZHpYsz6--_"
- }
- [ArangoError 1210: unique constraint violated - in index 16330 of type hash over ["a","b.c"]; conflicting key: 16339]
Hide execution results
Ensures that a non-unique hash index exists: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
:
In case that the index was successfully created, an object with the indexdetails, including the index-identifier, is returned.
Show execution results
- {
- "deduplicate" : true,
- "fields" : [
- "a"
- ],
- "id" : "test/16233",
- "isNewlyCreated" : true,
- "selectivityEstimate" : 1,
- "sparse" : false,
- "type" : "hash",
- "unique" : false,
- "code" : 201
- }
- {
- "_id" : "test/16236",
- "_key" : "16236",
- "_rev" : "_ZHpYsf2--_"
- }
- {
- "_id" : "test/16240",
- "_key" : "16240",
- "_rev" : "_ZHpYsf6--_"
- }
- {
- "_id" : "test/16243",
- "_rev" : "_ZHpYsf6--B"
- }
Hide execution results
Creates a non-unique hash array index for the individual elements of the arrayattributes field1[]
, … fieldn[
]
found in the documents. At leastone attribute path has to be given. The index always treats the indexed arrays assparse.
It is possible to combine array indexing with standard indexing:collection.ensureIndex({ type: "hash", fields: [ "field1[*]", "field2" ] })
In case that the index was successfully created, an object with the indexdetails, including the index-identifier, is returned.
Show execution results
- {
- "deduplicate" : true,
- "fields" : [
- "a[*]"
- ],
- "id" : "test/16250",
- "isNewlyCreated" : true,
- "selectivityEstimate" : 1,
- "sparse" : false,
- "type" : "hash",
- "unique" : false,
- "code" : 201
- }
- {
- "_id" : "test/16253",
- "_key" : "16253",
- "_rev" : "_ZHpYsi2--_"
- }
- {
- "_id" : "test/16257",
- "_key" : "16257",
- "_rev" : "_ZHpYsi2--B"
- }
- {
- "_id" : "test/16260",
- "_key" : "16260",
- }
Hide execution results
It is possible to create secondary indexes using the edge attributes _from
and _to
, starting with ArangoDB 3.0. A combined index over both fields togetherwith the unique option enabled can be used to prevent duplicate relations frombeing created.
For example, a document collection verts might contain vertices with the documenthandles verts/A
, and verts/C
. Relations between these documents canbe stored in an edge collection edges for instance. Now, you may want to make surethat the vertex verts/A
is never linked to verts/B
by an edge more than once.This can be achieved by adding a unique, non-sparse hash index for the fields _from
and _to
:
Creating an edge { from: "verts/A", _to: "verts/B" }
in _edges will be accepted,but only once. Another attempt to store an edge with the relation A → B willbe rejected by the server with a unique constraint violated error. This includesupdates to the _from
and _to
fields.
Note that adding a relation B → A is still possible, so is A → Aand B → B, because they are all different relations in a directed graph.Each one can only occur once however.