The most recent versions of ArangoDB offer more detailed control over what values will be auto-generated for the _key and _id attributes of documents. They also allow using predictable auto-increment sequences.

ArangoDB 1.1 and before always auto-generated the values for the _id attribute of documents, and since ArangoDB 1.2 there was the possibility to provide user-defined document keys in the _key attribute.

ArangoDB 1.2.2 extends this a bit more: each collection now has a set of so-called “key options”, which control if and how document keys are generated, and if users are allowed to provide their own _key values.

“Traditional” key generator

When no “key options” are specified on collection creation, ArangoDB will use the “traditional” key generator by default.

This key generator will work in the same ways as ArangoDB 1.2 previously did. Each collection will happily accept user-defined _key values. If the user doesn’t specify any _key value, the server will auto-generate the value. The auto-generated _key values will be ever-increasing numbers, with a non-deterministic increment:

“Autoincrement” key generator

To get better control over the generated _key values, there is the “autoincrement” key generator. This key generator will generate much “nicer” and predictable keys. It can activated as follows when a collection is created:

The start value and the actual increment values for the sequence can optionally be adjusted when the collection is first created:

Auto-generated and user-specified keys can also be mixed. We’ll now create a document using our own key, and after that create a document and let the server generate the next _key value:

Note that the auto-generated _key value of “29” is in line with the auto-increment rules initially specified (offset 5, increment 12).

Disallowing user-defined keys

In some cases it may make sense to give the server the sole responsibility for generating the _key values, and to disallow user-supplied keys completely. This is supported in ArangoDB by creating a collection and setting the “allowUserKeys” attribute to false. Whenever there is an attempt then to insert a document with a user-defined key, the server will reject it:

As noted above, the feature has been made available in recent versions of ArangoDB 1.2, and is thus contained in the ArangoDB 1.2.2 builds, which are available for download here. Enjoy!