4

I had created a collection in ArangoDB and need to say that one field is unique. For example I need to say that in 'user_table' 'email' is unique. How to do that?

stj
  • 9,037
  • 19
  • 33
AliPrf
  • 389
  • 2
  • 10

1 Answers1

5

To ensure uniqueness of a certain attribute in a collection, you can use the ensureUniqueConstraint function for the collection:

db['user_table'].ensureUniqueConstraint("email");

This will create a non-sparse unique index on attribute email.

If email is an optional attribute, you may want to go with:

db['user_table'].ensureUniqueConstraint("email", { sparse: true });

As @CoDEmanX mentioned, it's also possible to use the more general ensureIndex method and specify index type and uniqueness as parameters:

db['user_table'].ensureIndex({ fields: ["email"], type: "hash", unique: true, sparse: true });
stj
  • 9,037
  • 19
  • 33
  • 1
    Alternatively, `db['user_table'].ensureIndex("field", {unique: true});` can be used for the [exact same result](https://docs.arangodb.com/IndexHandling/Hash.html). Sub-attributes are also support, but not array elements in a certain position I suppose (@stj?) – CodeManX Jun 08 '15 at 08:15
  • That is true. At the moment we are looking into if & how this can be extended. – stj Jun 09 '15 at 15:39