Collections

Collections allow you to store documents and you can use them to group records of similar kinds together

A collection can contain a set of documents, similar to how a folder contains files. You can store documents with varying data structures in a single collection, but a collection is typically used to only store one type of entities. For example, you can use one collection for products, another for customers, and yet another for orders.

Collection types

  • The regular type of collection is a document collection. If you use document collections for a graph, then they are referred to as vertex collections.

  • To store connections between the vertices of a graph, you need to use edge collections. The documents they contain have a _from and a _to attribute to reference documents by their ID.

  • Collection that are used internally by ArangoDB are prefixed with an underscore (like _users) and are called system collections. They can be document collections as well as edge collections.

You need to specify whether you want a document collection or an edge collection when you create the collection. The type cannot be changed later.

Collection names

You can give each collection a name to identify and access it. The name needs to be unique within a database, but not globally for the entire ArangoDB instance.

The namespace for collections is shared with Views. There cannot exist a collection and a View with the same name in the same database.

The collection name needs to be a string that adheres to either the traditional or the extended naming constraints. Whether the former or the latter is active depends on the --database.extended-names startup option. The extended naming constraints are used if enabled, allowing many special and UTF-8 characters in database, collection, View, and index names. If set to false (default), the traditional naming constraints are enforced.

The extended naming constraints are an experimental feature but they will become the norm in a future version. Check if your drivers and client applications are prepared for this feature before enabling it.

The restrictions for collection names are as follows:

  • For the traditional naming constraints:

    • The names must only consist of the letters A to Z (both in lower and upper case), the digits 0 to 9, and underscore (_) and dash (-) characters. This also means that any non-ASCII names are not allowed.
    • Names of user-defined collections must always start with a letter. System collection names must start with an underscore. You should not use system collection names for your own collections.
    • The maximum allowed length of a name is 256 bytes.
    • Collection names are case-sensitive.
  • For the extended naming constraints:

    • Names can consist of most UTF-8 characters, such as Japanese or Arabic letters, emojis, letters with accentuation. Some ASCII characters are disallowed, but less compared to the traditional naming constraints.
    • Names cannot contain the characters / or : at any position, nor any control characters (below ASCII code 32), such as \n, \t, \r, and \0.
    • Spaces are accepted, but only in between characters of the name. Leading or trailing spaces are not allowed.
    • . (dot), _ (underscore) and the numeric digits 0-9 are not allowed as first character, but at later positions.
    • Collection names are case-sensitive.
    • Collection names containing UTF-8 characters must be NFC-normalized . Non-normalized names are rejected by the server.
    • The maximum length for a collection name is 256 bytes after normalization. As a UTF-8 character may consist of multiple bytes, this does not necessarily equate to 256 characters.

    Example collection names that can be used with the extended naming constraints: España, 😀, , كلب, @abc123, København, München, Бишкек, abc? <> 123!

While it is possible to change the value of the --database.extended-names option from false to true to enable extended names, the reverse is not true. Once the extended names have been enabled, they remain permanently enabled so that existing databases, collections, Views, and indexes with extended names remain accessible.

Please be aware that dumps containing extended names cannot be restored into older versions that only support the traditional naming constraints. In a cluster setup, it is required to use the same naming constraints for all Coordinators and DB-Servers of the cluster. Otherwise, the startup is refused.

You can rename collections (except in cluster deployments). This changes the collection name, but not the collection identifier.

Collection identifiers

A collection identifier lets you refer to a collection in a database, similar to the name. It is a string value and is unique within a database. Unlike collection names, ArangoDB assigns collection IDs automatically and you have no control over them.

ArangoDB internally uses collection IDs to look up collections. However, you should use the collection name to access a collection instead of its identifier.

ArangoDB uses 64-bit unsigned integer values to maintain collection IDs internally. When returning collection IDs to clients, ArangoDB returns them as strings to ensure the identifiers are not clipped or rounded by clients that do not support big integers. Clients should treat the collection IDs returned by ArangoDB as opaque strings when they store or use them locally.

Key generators

ArangoDB allows using key generators for each collection. Key generators have the purpose of auto-generating values for the _key attribute of a document if none was specified by the user. By default, ArangoDB uses the traditional key generator. The traditional key generator auto-generates key values that are strings with ever-increasing numbers. The increment values it uses are non-deterministic.

Contrary, the auto-increment key generator auto-generates deterministic key values. Both the start value and the increment value can be defined when the collection is created. The default start value is 0 and the default increment is 1, meaning the key values it creates by default are:

1, 2, 3, 4, 5, …

When creating a collection with the auto-increment key generator and an increment of 5, the generated keys would be:

1, 6, 11, 16, 21, …

The auto-increment values are increased and handed out on each document insert attempt. Even if an insert fails, the auto-increment value is never rolled back. That means there may exist gaps in the sequence of assigned auto-increment values if inserts fails.

Synchronous replication of collections

Distributed ArangoDB setups offer synchronous replication, which means that there is the option to replicate all data automatically within an ArangoDB cluster. This is configured for sharded collections on a per-collection basis by specifying a replication factor. A replication factor of k means that altogether k copies of each shard are kept in the cluster on k different servers, and are kept in sync. That is, every write operation is automatically replicated on all copies.

This is organized using a leader/follower model. At all times, one of the servers holding replicas for a shard is “the leader” and all others are “followers”, this configuration is held in the Agency (see Cluster for details of the ArangoDB cluster architecture). Every write operation is sent to the leader by one of the Coordinators, and then replicated to all followers before the operation is reported to have succeeded. The leader keeps a record of which followers are currently in sync. In case of network problems or a failure of a follower, a leader can and will drop a follower temporarily after 3 seconds, such that service can resume. In due course, the follower will automatically resynchronize with the leader to restore resilience.

If a leader fails, the cluster Agency automatically initiates a failover routine after around 15 seconds, promoting one of the followers to leader. The other followers (and the former leader, when it comes back), automatically resynchronize with the new leader to restore resilience. Usually, this whole failover procedure can be handled transparently for the Coordinator, such that the user code does not even see an error message.

This fault tolerance comes at a cost of increased latency. Each write operation needs an additional network roundtrip for the synchronous replication of the followers (but all replication operations to all followers happen concurrently). Therefore, the default replication factor is 1, which means no replication.

Collection interfaces

The following sections show examples of how you can use the APIs of ArangoDB and the official drivers, as well as the ArangoDB Shell and the built-in web interface, to perform common operations related to collections. For less common operations and other drivers, see the corresponding reference documentation.

Create a collection

  1. Click Collections in the main navigation.
  2. Click Add collection.
  3. Set a Name and optionally configuration options.
  4. Click Create.
coll = db._create("coll");
Show output

See db._create() in the JavaScript API for details.

curl -d '{"name":"coll"}' http://localhost:8529/_db/mydb/_api/collection

See the POST /_db/{database-name}/_api/collection endpoint in the HTTP API for details.

let coll = await db.createCollection("coll");
// -- or --
coll = db.collection("coll");
const info = await coll.create();

See Database.createCollection()  and DocumentCollection.create()  in the arangojs documentation for details.

ctx := context.Background()
coll, err := db.CreateCollection(ctx, "coll", nil)
if err != nil {
  fmt.Println(err)
} else {
  _ = coll // Use coll here
}

See DatabaseCollection.CreateCollection()  in the go-driver v2 documentation for details.

CollectionEntity coll = db.createCollection("coll");
// -- or --
CollectionEntity coll = db.collection("coll").create();

See ArangoDB.createCollection()  and ArangoCollection.create()  in the arangodb-java-driver documentation for details.

coll = db.create_collection("coll")

See StandardDatabase.create_collection()  in the python-arango documentation for details.

Get a collection

  1. If necessary, switch to the database that contains the desired collection.
  2. Click Collections in the main navigation.
  3. Click the name or row of the desired collection.
coll = db._collection("coll");
Show output

There is a short-cut that you can use:

let coll = db.coll
// or
let coll = db["coll"]

See db._collection() in the JavaScript API for details.

curl http://localhost:8529/_db/mydb/_api/collection/coll

See the GET /_db/{database-name}/_api/collection/{collection-name} endpoint in the HTTP API for details.

let coll = db.collection("coll");
const info = await coll.get();

See Database.collection()  in the arangojs documentation for details.

ctx := context.Background()
coll, err := db.GetCollection(ctx, "coll", nil)
if err != nil {
  fmt.Println(err)
} else {
  _ = coll // Use coll here
}

See DatabaseCollection.GetCollection()  in the go-driver v2 documentation for details.

ArangoCollection coll = db.collection("coll");
CollectionEntity info = coll.getInfo();

See ArangoDB.collection()  and ArangoCollection.getInfo()  in the arangodb-java-driver documentation for details.

coll = db.collection("coll")

See StandardDatabase.collection()  in the python-arango documentation for details.

List all collections

  1. If necessary, switch to the database that you want to list the collection of.
  2. Click Collections in the main navigation.
  3. All collections are listed, given that no Filters are applied and you have at least read access for all collections.
db._collections();
Show output

See db._collections() in the JavaScript API for details.

curl http://localhost:8529/_db/mydb/_api/collection

See the GET /_db/{database-name}/_api/collection endpoint in the HTTP API for details.

const colls = await db.collections();
colls.forEach(c => console.log(c.name))

See Database.collections()  in the arangojs documentation for details.

ctx := context.Background()
colls, err := db.Collections(ctx)
if err != nil {
  fmt.Println(err)
} else {
  for _, c := range colls {
    fmt.Println(c.Name())
  }
}

See DatabaseCollection.Collections()  in the go-driver v2 documentation for details.

Collection<CollectionEntity> colls = db.getCollections();
colls.forEach(c -> System.out.println(c.getName()));

See ArangoDatabase.getCollections()  in the arangodb-java-driver documentation for details.

colls = db.collections()
for c in colls:
  print(c["name"])

See StandardDatabase.collections()  in the python-arango documentation for details.

Get the collection properties

  1. If necessary, switch to the database that contains the desired collection.
  2. Click Collections in the main navigation.
  3. Click the name or row of the desired collection.
  4. The properties are listed in the Info, Computed Values, and Schema tabs.
var coll = db._collection("coll");
coll.properties();
Show output

See collection.properties() in the JavaScript API for details.

curl http://localhost:8529/_db/mydb/_api/collection/coll/properties

See the GET /_db/{database-name}/_api/collection/{collection-name}/properties endpoint in the HTTP API for details.

let coll = db.collection("coll");
const props = await coll.properties();
console.log(props);

See DocumentCollection.properties()  in the arangojs documentation for details.

ctx := context.Background()
coll, err := db.GetCollection(ctx, "coll", nil)
if err != nil {
  fmt.Println(err)
} else {
  props, err := coll.Properties(ctx)
  if err != nil {
    fmt.Println(err)
  } else {
    fmt.Printf("%+v", props)
  }
}

See Collection.Properties()  in the go-driver v2 documentation for details.

ArangoCollection coll = db.collection("coll");
CollectionPropertiesEntity props = coll.getProperties();

See ArangoCollection.getProperties()  in the arangodb-java-driver documentation for details.

coll = db.collection("coll")
props = coll.properties()

See Collection.properties()  in the python-arango documentation for details.

Set the collection properties

  1. If necessary, switch to the database that contains the desired collection.
  2. Click Collections in the main navigation.
  3. Click the name or row of the desired collection.
  4. The properties you can change are listed in the Settings, Computed Values, and Schema tabs.
  5. Make the desired changes and click the Save button.
var coll = db._collection("coll");
coll.properties({
  waitForSync: true,
  replicationFactor: 3
});
Show output

See collection.properties() in the JavaScript API for details.

curl -XPUT -d '{"waitForSync":true,"replicationFactor":3}' http://localhost:8529/_db/mydb/_api/collection/coll/properties

See the PUT /_db/{database-name}/_api/collection/{collection-name}/properties endpoint in the HTTP API for details.

let coll = db.collection("coll");
const props = await coll.properties({
  waitForSync: true,
  replicationFactor: 3
});

See DocumentCollection.properties()  in the arangojs documentation for details.

ctx := context.Background()
coll, err := db.GetCollection(ctx, "coll", nil)
if err != nil {
  fmt.Println(err)
} else {
  err := coll.SetProperties(ctx, arangodb.SetCollectionPropertiesOptions{
    WaitForSync:       utils.NewType(true),
    ReplicationFactor: 3,
  })
  if err != nil {
    fmt.Println(err)
  }
}

See Collection.SetProperties()  in the go-driver v2 documentation for details.

CollectionPropertiesOptions options = new CollectionPropertiesOptions()
  .waitForSync(true)
  .replicationFactor(ReplicationFactor.of(3));

ArangoCollection coll = db.collection("coll");
CollectionPropertiesEntity props = coll.changeProperties(options);

See ArangoCollection.changeProperties()  and CollectionPropertiesEntity  in the arangodb-java-driver documentation for details.

coll = db.collection("coll")
props = coll.configure(
  sync=True,
  replication_factor=3
)

See Collection.configure()  in the python-arango documentation for details.

Remove a collection

  1. If necessary, switch to the database that contains the desired collection.
  2. Click Collections in the main navigation.
  3. Click the name or row of the desired collection.
  4. Go to the Settings tab.
  5. Click the Delete button and confirm the deletion.
db._drop("coll");
Show output

See db._drop() in the JavaScript API for details.

curl -XDELETE http://localhost:8529/_db/mydb/_api/collection/coll

See the DELETE /_db/{database-name}/_api/collection/{collection-name} endpoint in the HTTP API for details.

let coll = db.collection("coll");
const status = await coll.drop();

See DocumentCollection.drop()  in the arangojs documentation for details.

ctx := context.Background()
coll, err := db.GetCollection(ctx, "coll", nil)
if err != nil {
  fmt.Println(err)
} else {
  err = coll.Remove(ctx)
  if err != nil {
    fmt.Println(err)
  }
}

See Collection.Remove()  in the go-driver v2 documentation for details.

ArangoCollection coll = db.collection("coll");
coll.drop();

See ArangoCollection.drop()  in the arangodb-java-driver documentation for details.

ok = db.delete_collection("coll")

See StandardDatabase.delete_collection()  in the python-arango documentation for details.