【发布时间】:2015-05-28 06:48:02
【问题描述】:
我在 ArangoDB 中创建了一个集合,需要说一个字段是唯一的。例如,我需要说在 'user_table' 中,'email' 是唯一的。 怎么做?
【问题讨论】:
标签: database unique arangodb aql
我在 ArangoDB 中创建了一个集合,需要说一个字段是唯一的。例如,我需要说在 'user_table' 中,'email' 是唯一的。 怎么做?
【问题讨论】:
标签: database unique arangodb aql
为保证集合中某个属性的唯一性,可以对集合使用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 });
【讨论】:
db['user_table'].ensureIndex("field", {unique: true}); 可用于exact same result。子属性也支持,但我想不是某个位置的数组元素(@stj?)