我在 ArangoDB 中创建了一个集合,需要说一个字段是唯一的。例如,我需要说在 'user_table' 中,'email' 是唯一的。怎么做?
问问题
825 次
1 回答
5
为了确保集合中某个属性的唯一性,可以使用该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 });
于 2015-05-28T07:01:33.633 回答