【问题标题】:Create unique index in ArangoDB edge collection for multiple path attributes including _from and _to attributes在 ArangoDB 边缘集合中为多个路径属性(包括 _from 和 _to 属性)创建唯一索引
【发布时间】:2017-03-25 02:30:17
【问题描述】:

我正在尝试为边缘集合设置唯一约束,以便在两个给定节点之间只能创建某种类型的边缘。问题是,在创建索引时,我似乎不能使用_from_to 属性作为路径属性。到目前为止我尝试过的:

db._collection('edges').ensureUniqueConstraint('_from', '_to', 'type');

我收到以下错误:

[ArangoError 10: bad parameter]

我不想在创建它之前检查两个节点之间是否存在某种边缘类型。

有什么提示吗?

【问题讨论】:

    标签: arangodb


    【解决方案1】:

    目前无法在 _key、_id、_rev、_from 或 _to 等内部属性上创建二级索引。我们希望在 ArangoDB 的未来版本中允许这样做,但这将是一个巨大的代码更改。

    实现所需结果的唯一方法是在您保存的边缘中创建一个额外的属性,并将“_from”、“_to”和“type”的组合放入其中。我认为这些值应该在边缘创建时就已经知道了。

    所以不要像这样保存边缘

    db.edges.save(_from, _to, { type: type, other: ... });
    

    应该是这样的:

    // create a unique index on attribute "unique"
    db._collection("edges").ensureUniqueConstraint("unique");
    
    // create a variable "unique" which contains the values of _from and _to and type
    var unique = _from + "-" + _to + "-" + String(type);
    
    // now save the edge, using the "unique" attribute
    db.edges.save(_from, _to, { type: type, unique: unique, other: ... });
    

    这是一种解决方法,但它应该可以解决该特定问题。

    【讨论】:

    • 感谢您指出这一点(希望确保此功能尚不可用)。它应该可以解决问题,谢谢!
    【解决方案2】:

    从 ArangoDB 3.0 开始,您可以在 ensure uniqueness of relations in edge collections 上使用 _from_to 字段上的唯一哈希索引。

    db.edgeCollectionName.ensureIndex({ type: "hash", fields: [ "_from", "_to" ], unique: true });

    当存在 A->B 关系时,不会创建另一个 A->BB->A 仍然可以创建。

    【讨论】:

    • 而且您始终可以在创建哈希之前对 A 和 B 进行排序,以使其适用于 A->B 和 B->A。
    猜你喜欢
    • 2023-04-04
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多