Persistent indexes

    This is an introduction to ArangoDB’s persistent indexes.

    It is possible to define a persistent index on one or more attributes (or paths)of documents. The index is then used in queries to locate documents within a given range. If the index is declared 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 .

    ensures that a unique persistent index existscollection.ensureIndex({ type: "persistent", fields: [ "field1", …, "fieldn" ], unique: true })

    Creates a unique persistent index on all documents using field1, … _fieldn_as attribute paths. At least one attribute path has to be given. The index willbe 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: "persistent", 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 in any of the specified index attributes. Such documents willnot be indexed, and not be taken into account for uniqueness checks.

    In a non-sparse index, these documents will be indexed (for non-presentindexed attributes, a value of null will be used) and will be taken intoaccount for uniqueness checks.

    Show execution results

    Hide execution results

    1. {
    2. "deduplicate" : true,
    3. "fields" : [
    4. "myId"
    5. ],
    6. "id" : "ids/74827",
    7. "isNewlyCreated" : true,
    8. "name" : "idx_1655126002667356160",
    9. "selectivityEstimate" : 1,
    10. "sparse" : false,
    11. "type" : "persistent",
    12. "unique" : true,
    13. "code" : 201
    14. }
    15. {
    16. "_id" : "ids/74831",
    17. "_key" : "74831",
    18. "_rev" : "_Z2KDQFi---"
    19. }
    20. {
    21. "_id" : "ids/74833",
    22. "_key" : "74833",
    23. "_rev" : "_Z2KDQFi--A"
    24. }
    25. {
    26. "_id" : "ids/74835",
    27. "_key" : "74835",
    28. "_rev" : "_Z2KDQFm---"
    29. }
    30. [ArangoError 1210: unique constraint violated - in index idx_1655126002667356160 of type persistent over 'myId'; conflicting key: 74831]
    1. arangosh> db.ids.ensureIndex({ type: "persistent", fields: [ "name.first", "name.last" ], unique: true });
    2. arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
    3. arangosh> db.ids.save({ "name" : { "first" : "jens", "last": "jensen" }});
    4. arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});

    Show execution results

    Hide execution results

    1. {
    2. "deduplicate" : true,
    3. "fields" : [
    4. "name.first",
    5. "name.last"
    6. ],
    7. "id" : "ids/74808",
    8. "isNewlyCreated" : true,
    9. "name" : "idx_1655126002658967552",
    10. "selectivityEstimate" : 1,
    11. "sparse" : false,
    12. "type" : "persistent",
    13. "unique" : true,
    14. "code" : 201
    15. }
    16. {
    17. "_id" : "ids/74812",
    18. "_key" : "74812",
    19. "_rev" : "_Z2KDQFC---"
    20. }
    21. {
    22. "_id" : "ids/74814",
    23. "_key" : "74814",
    24. "_rev" : "_Z2KDQFC--A"
    25. }
    26. {
    27. "_id" : "ids/74816",
    28. "_key" : "74816",
    29. "_rev" : "_Z2KDQFG---"
    30. }
    31. [ArangoError 1210: unique constraint violated - in index idx_1655126002658967552 of type persistent over 'name.first, name.last'; conflicting key: 74812]

    ensures that a non-unique persistent index existscollection.ensureIndex({ type: "persistent", fields: [ "field1", …, "fieldn" ] })

    Creates a non-unique persistent 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

    Hide execution results

    1. {
    2. "deduplicate" : true,
    3. "fields" : [
    4. "first"
    5. ],
    6. "isNewlyCreated" : true,
    7. "name" : "idx_1655126002580324354",
    8. "selectivityEstimate" : 1,
    9. "sparse" : false,
    10. "type" : "persistent",
    11. "unique" : false,
    12. "code" : 201
    13. }
    14. {
    15. "_id" : "names/74523",
    16. "_key" : "74523",
    17. "_rev" : "_Z2KDQAW---"
    18. }
    19. {
    20. "_id" : "names/74525",
    21. "_key" : "74525",
    22. "_rev" : "_Z2KDQAa---"
    23. }
    24. {
    25. "_id" : "names/74527",
    26. "_key" : "74527",
    27. "_rev" : "_Z2KDQAa--A"
    28. }
    29. {
    30. "_id" : "names/74529",
    31. "_key" : "74529",
    32. "_rev" : "_Z2KDQAe---"
    33. }
    34. {
    35. "_id" : "names/74531",
    36. "_key" : "74531",
    37. "_rev" : "_Z2KDQAe--A"
    38. }

    Selects all documents from the collection that match the specified example and returns a cursor. A persistent index will be used if present.

    You can use toArray, next, or hasNext to access theresult. 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

    1. { "a" : { "c" : 1 } }

    as example, then you will find all documents, such that the attributea contains a document of the form {c : 1 }. For example the document

    1. { "a" : { "c" : 1 }, "b" : 1 }

    will match, but the document

    will not.

    However, if you use

    1. { "a.c" : 1 },

    then you will find all documents, which contain a sub-document in a_that has an attribute _c of value 1. Both the following documents

    and

      will match.

      To fix persistent indexes after a language change, delete and re-create them.Skiplist indexes are not affected, because they are not persisted andautomatically rebuilt on every server start.