Sunday 26 September 2021

ArangoDB: Index identifier, handle and name

Index handle is used to uniquely identify an index within the database.

 

For example, let’s create an "employees" collection and find out all the indexes on it.

127.0.0.1:8529@abc_org> db._create("employees")
[ArangoCollection 77247, "employees" (type document, status loaded)]

127.0.0.1:8529@abc_org> db.employees.getIndexes()
[ 
  { 
    "fields" : [ 
      "_key" 
    ], 
    "id" : "employees/0", 
    "name" : "primary", 
    "selectivityEstimate" : 1, 
    "sparse" : false, 
    "type" : "primary", 
    "unique" : true 
  } 
]

As you see the above snippet, "employees/0" is the index handle, it is a string and consists of the collection name and an index identifier separated by a /.

 

Access the index using index handle

Index handle is unique within the database. You can access it using _index method.

 

Syntax

db._index("<index-handle>");


Example

index1 = db._index("employees/0")

127.0.0.1:8529@abc_org> index1 = db._index("employees/0")
{ 
  "fields" : [ 
    "_key" 
  ], 
  "id" : "employees/0", 
  "name" : "primary", 
  "sparse" : false, 
  "type" : "primary", 
  "unique" : true, 
  "code" : 200 
}


You can access the values of index using . operator.

127.0.0.1:8529@abc_org> index1.name
primary

127.0.0.1:8529@abc_org> index1.type
primary

127.0.0.1:8529@abc_org> index1.fields
[ 
  "_key" 
]


You can even use the index method on collection to get the index by index handle.

db.collection.index("<index-handle>");

127.0.0.1:8529@abc_org> db.employees.index("employees/0")
{ 
  "fields" : [ 
    "_key" 
  ], 
  "id" : "employees/0", 
  "name" : "primary", 
  "sparse" : false, 
  "type" : "primary", 
  "unique" : true, 
  "code" : 200 
}


Access index using index identifier

Index identifier is unique within the collection.

 

Syntax

db.collection.index("<index-identifier>");

 

Example

127.0.0.1:8529@abc_org> db.employees.index("0")
{ 
  "fields" : [ 
    "_key" 
  ], 
  "id" : "employees/0", 
  "name" : "primary", 
  "sparse" : false, 
  "type" : "primary", 
  "unique" : true, 
  "code" : 200 
}


Accessing indexes using index name

Index names are unique within the collection.

 

Syntax

db._index("collectionName/indexName")

db.demo.index("indexName")

127.0.0.1:8529@abc_org> db._index("employees/primary")
{ 
  "fields" : [ 
    "_key" 
  ], 
  "id" : "employees/0", 
  "name" : "primary", 
  "sparse" : false, 
  "type" : "primary", 
  "unique" : true, 
  "code" : 200 
}

127.0.0.1:8529@abc_org> db.employees.index("primary")
{ 
  "fields" : [ 
    "_key" 
  ], 
  "id" : "employees/0", 
  "name" : "primary", 
  "sparse" : false, 
  "type" : "primary", 
  "unique" : true, 
  "code" : 200 
}



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment