[英]Create unique index in ArangoDB edge collection for multiple path attributes including _from and _to attributes
我正在尝试为边集合设置唯一约束,以便在两个给定节点之间只能创建某种类型的一个边。 问题是,在创建索引时,我似乎无法使用_from
和_to
属性作为路径属性。 到目前为止我尝试了什么:
db._collection('edges').ensureUniqueConstraint('_from', '_to', 'type');
我收到以下错误:
[ArangoError 10: bad parameter]
我不想在创建它之前检查两个节点之间是否存在某种边缘类型。
任何提示?
目前无法在内部属性(如_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: ... });
这是一种解决方法,但它应该解决该特定问题。
从ArangoDB 3.0开始,您可以使用_from
和_to
字段上的唯一哈希索_from
确保边集合中关系的唯一性 。
db.edgeCollectionName.ensureIndex({ type: "hash", fields: [ "_from", "_to" ], unique: true });
当存在A-> B关系时,将不会创建另一个A-> B. 但是仍然可以创建B-> A.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.