如何确保文档中的字段在ArangoDB中是唯一的?

时间:2015-05-28 06:48:02

标签: database unique arangodb aql

我在ArangoDB中创建了一个集合,需要说一个字段是唯一的。例如,我需要说'user_table''电子邮件'是唯一的。 怎么做?

1 个答案:

答案 0 :(得分:4)

为确保集合中某个属性的唯一性,您可以使用ensureUniqueConstraint函数进行集合:

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

这将在属性email上创建非稀疏的唯一索引。

如果email是可选属性,您可能需要使用:

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

正如@CoDEmanX所提到的,它也可以使用更通用的ensureIndex方法并指定索引类型和唯一性作为参数:

db['user_table'].ensureIndex({ fields: ["email"], type: "hash", unique: true, sparse: true });
相关问题