简体   繁体   中英

How to ensure that a field in a document is unique in ArangoDB?

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?

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 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM