【问题标题】:How to ensure that a field in a document is unique in ArangoDB?如何确保文档中的字段在 ArangoDB 中是唯一的?
【发布时间】:2015-05-28 06:48:02
【问题描述】:

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

【问题讨论】:

    标签: database unique arangodb aql


    【解决方案1】:

    为保证集合中某个属性的唯一性,可以对集合使用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?)
    • 确实如此。目前,我们正在研究是否可以扩展以及如何扩展。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    相关资源
    最近更新 更多