简体   繁体   English

ArangoDB - 使用 AQL 交换边缘的 _to 和 _from 值

[英]ArangoDB - Swap _to and _from values for edge using AQL

Is there a clean way to swap the _to and _from values for an edge using AQL?有没有一种干净的方法可以使用 AQL 将_to_from值交换为边缘? According to Arango's documentation on Edges :根据Arango 关于 Edges 的文档

To change edge endpoints you would need to remove old document/edge and insert new one.要更改边缘端点,您需要删除旧文档/边缘并插入新的。 Other fields can be updated as in default collection.其他字段可以像默认集合一样更新。

So what I was able to come up with was a query that looks like this:所以我能想出的是一个看起来像这样的查询:

FOR edge IN edge_collection
    FILTER [some criteria]
    LET tempEdge = KEEP(edge, ATTRIBUTES(edge, true))
    LET newEdge = MERGE([{'_key':edge._key}, {'_from':edge._to}, {'_to':edge._from}, tempEdge])
    REPLACE newEdge IN edge_collection
    RETURN NEW

To explain my own solution a bit, I used the ATTRIBUTES(edge, true) function to get the names of all of the Attributes on the Edge, and the true parameter removed the internal attributes (like _key , _id , _to , etc.).为了稍微解释一下我自己的解决方案,我使用了ATTRIBUTES(edge, true) function 来获取 Edge 上所有属性的名称,而true参数删除了内部属性(如_key_id_to等) . Read more about ATTRIBUTES here . 在此处阅读有关ATTRIBUTES的更多信息。

Then the KEEP(edge, [attributes]) function returns a new Document that only has the Attributes specified in the given array, which thanks to the ATTRIBUTES function in this case, is everything but the internal fields.然后KEEP(edge, [attributes]) function 返回一个新文档,该文档仅具有给定数组中指定的属性,这要归功于ATTRIBUTES function 在这种情况下,除了内部字段之外的所有内容。 Read more about KEEP here . 在此处阅读有关KEEP的更多信息。

Then I use the MERGE function to combine the _key from the original edge, swap the _to and _from values, and all of the non-internal attributes.然后我使用MERGE function 组合原始边缘的_key ,交换_to_from值以及所有非内部属性。 Read more about MERGE here . 在此处阅读有关MERGE的更多信息。

Lastly, I use REPLACE which removes the original edge and adds the new one in, just like Arango requires.最后,我使用REPLACE删除原始边缘并添加新边缘,就像 Arango 需要的那样。 Read more about REPLACE here . 在此处阅读有关REPLACE的更多信息。

Like I said, this appears to work, but the MERGE in particular feels like the wrong way to go about doing what I did.就像我说的,这似乎可行,但特别是MERGE感觉就像 go 做我所做的事情的错误方式。 Is there an easier way to set values on an Object?有没有更简单的方法在 Object 上设置值? For instance, something that would let me just make a call similar to: tempEdge._from = edge._to ?例如,可以让我拨打类似于以下内容的电话: tempEdge._from = edge._to

Yes, there is a simpler solution:是的,有一个更简单的解决方案:

FOR edge IN edge_collection
    FILTER [some criteria]
    UPDATE edge WITH {_from: edge._to, _to: edge._from} IN edge_collection
    RETURN NEW

_from and _to can be updated (in contrast to the system attributes _id , _key and _rev ), so you don't need to replace the whole document. _from_to可以更新(与系统属性_id_key_rev ),因此您不需要替换整个文档。 And since UPDATE merges the changes into the existing document, you only need to specify the new values for _from and _to .由于UPDATE将更改合并到现有文档中,因此您只需为_from_to指定新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM