在ArangoDB边缘集合中为多个路径属性创建唯一索引,包括_from和_to属性

时间:2014-07-31 08:31:23

标签: arangodb

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

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

我收到以下错误:

[ArangoError 10: bad parameter]

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

任何提示?

2 个答案:

答案 0 :(得分:5)

目前无法在内部属性(如_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: ... });

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

答案 1 :(得分:4)

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

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

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

相关问题