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
Skip-Lists
Introduction to Skiplist Indexes
This is an introduction to ArangoDB’s skiplists.
It is possible to define a skiplist index on one or more attributes (or paths) of a documents. This skiplist is then used in queries to locate documents within a given range. If the skiplist 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 skiplist 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 Skiplist Indexes from the Shell
collection.ensureIndex({ type: "skiplist", fields: [ "field1", ..., "fieldn" ], unique: true })
Creates a unique skiplist 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: "skiplist", fields: [ "field1", ..., "fieldn" ], unique: true, sparse: true })
In a sparse index all documents will be excluded from the index that do not
contain at least one of the specified index attributes or that have a value
of null
in any of the specified index attributes. Such documents will
not be indexed, and not be taken into account for uniqueness checks.
In a non-sparse index, these documents will be indexed (for non-present
indexed attributes, a value of null
will be used) 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.ids.ensureIndex({ type: "skiplist", fields: [ "myId" ], unique: true });
{
"id" : "ids/1138378881",
"type" : "skiplist",
"fields" : [
"myId"
],
"unique" : true,
"sparse" : false,
"isNewlyCreated" : true,
"code" : 201
}
arangosh> db.ids.save({ "myId": 123 });
{
"_id" : "ids/1138641025",
"_rev" : "1138641025",
"_key" : "1138641025"
}
arangosh> db.ids.save({ "myId": 456 });
{
"_id" : "ids/1138837633",
"_rev" : "1138837633",
"_key" : "1138837633"
}
arangosh> db.ids.save({ "myId": 789 });
{
"_id" : "ids/1139034241",
"_rev" : "1139034241",
"_key" : "1139034241"
}
arangosh> db.ids.save({ "myId": 123 });
[ArangoError 1210: cannot create document, unique constraint violated]
arangosh> db.ids.ensureIndex({ type: "skiplist", fields: [ "myId" ], unique: true });
arangosh> db.ids.save({ "myId": 123 });
arangosh> db.ids.save({ "myId": 456 });
arangosh> db.ids.save({ "myId": 789 });
arangosh> db.ids.save({ "myId": 123 });
arangosh> db.ids.ensureIndex({ type: "skiplist", fields: [ "name.first", "name.last" ], unique: true });
{
"id" : "ids/1139755137",
"type" : "skiplist",
"fields" : [
"name.first",
"name.last"
],
"unique" : true,
"sparse" : false,
"isNewlyCreated" : true,
"code" : 201
}
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
{
"_id" : "ids/1140082817",
"_rev" : "1140082817",
"_key" : "1140082817"
}
arangosh> db.ids.save({ "name" : { "first" : "jens", "last": "jensen" }});
{
"_id" : "ids/1140279425",
"_rev" : "1140279425",
"_key" : "1140279425"
}
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "jensen" }});
{
"_id" : "ids/1140476033",
"_rev" : "1140476033",
"_key" : "1140476033"
}
[ArangoError 1210: cannot create document, unique constraint violated]
arangosh> db.ids.ensureIndex({ type: "skiplist", fields: [ "name.first", "name.last" ], unique: true });
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
arangosh> db.ids.save({ "name" : { "first" : "jens", "last": "jensen" }});
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "jensen" }});
collection.ensureIndex({ type: "skiplist", fields: [ "field1", ..., "fieldn" ] })
Creates a non-unique skiplist 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 index
details, including the index-identifier, is returned.
arangosh> db.names.ensureIndex({ type: "skiplist", fields: [ "first" ] });
{
"id" : "names/1135298689",
"type" : "skiplist",
"fields" : [
"first"
],
"unique" : false,
"sparse" : false,
"isNewlyCreated" : true,
"code" : 201
}
arangosh> db.names.save({ "first" : "Tim" });
{
"_id" : "names/1135560833",
"_rev" : "1135560833",
"_key" : "1135560833"
}
arangosh> db.names.save({ "first" : "Tom" });
{
"_id" : "names/1135757441",
"_rev" : "1135757441",
"_key" : "1135757441"
}
arangosh> db.names.save({ "first" : "John" });
{
"_id" : "names/1135954049",
"_rev" : "1135954049",
"_key" : "1135954049"
}
arangosh> db.names.save({ "first" : "Tim" });
{
"_id" : "names/1136150657",
"_rev" : "1136150657",
"_key" : "1136150657"
}
arangosh> db.names.save({ "first" : "Tom" });
{
"_id" : "names/1136347265",
"_rev" : "1136347265",
"_key" : "1136347265"
}
arangosh> db.names.ensureIndex({ type: "skiplist", fields: [ "first" ] });
arangosh> db.names.save({ "first" : "Tim" });
arangosh> db.names.save({ "first" : "Tom" });
arangosh> db.names.save({ "first" : "John" });
arangosh> db.names.save({ "first" : "Tim" });
arangosh> db.names.save({ "first" : "Tom" });
Query by example using a skiplist index
collection.byExampleSkiplist(index, example)
Selects all documents from the specified skiplist index that match the
specified example and returns a cursor.
You can use toArray, next, or hasNext to access the
result. The result can be limited using the skip and limit
operator.
An attribute name of the form a.b is interpreted as attribute path,
not as attribute. If you use
{ a : { c : 1 } }
as example, then you will find all documents, such that the attribute
a contains a document of the form {c : 1 }. For example the document
{ a : { c : 1 }, b : 1 }
will match, but the document
{ a : { c : 1, b : 1 } }
will not.
However, if you use
{ a.c : 1 },
then you will find all documents, which contain a sub-document in a
that has an attribute @LIT{c} of value 1. Both the following documents
{ a : { c : 1 }, b : 1 }and
{ a : { c : 1, b : 1 } }
will match.
collection.byExampleSkiplist(index, path1, value1, ...)