[英]How to find sum of all descendents attribute named x in ArangoDB?
As i have tree structure in my graph database ArangoDB like below因为我的图形数据库 ArangoDB 中有树结构,如下所示
The node with id 2699394 is a parent node of this graph tree. id 为 2699394 的节点是这个图树的父节点。 and each node has attribute named X assigned to it.
每个节点都有一个名为 X 的属性分配给它。 I want to know sum of x of all the descendants of parent node 2699394 exclusive of its own attribute x in sum.
我想知道父节点 2699394 的所有后代 x 的总和,不包括它自己的属性 x 的总和。 for example suppose if,
例如假设如果,
2699399 x value is = 5,
2699408 x value is = 3,
2699428 x value is = 2,
2699418 x value is = 5,
then parent node 2699394 sum of x should be = 5 + 3 + 2 + 5
= 15
so the answer is 15. So can anybody give me query for this calculation in ArangoDB AQL?所以答案是 15。那么有人可以在 ArangoDB AQL 中查询这个计算吗?
To find out no of descendants of particular node, i have used below query,为了找出特定节点的后代,我使用了以下查询,
`FOR v, e, p in 1..1000 OUTBOUND 'Person/1648954'
GRAPH 'Appes'
RETURN v.id`
Thanks in Advance.提前致谢。 Mayank
玛雅克
Assuming that children are linked to their parents, the data could be visualized like this:假设孩子与他们的父母有联系,数据可以像这样可视化:
nodes/2699394 SUM of children?
↑
nodes/2699399 {x: 5}
↑
nodes/2699408 {x: 3}
↑
nodes/2699428 {x: 2}
↑
nodes/2699418 {x: 5}
To walk the chain of children, we need to traverse in INBOUND direction (or OUTOBUND if parent nodes point to children):要遍历子链,我们需要在 INBOUND 方向遍历(如果父节点指向子节点,则为 OUTOBUND):
FOR v IN 1..10 INBOUND "nodes/2699394" relations
RETURN v
In this example, an anonymous graph is used by specifying an edge collection relations .在此示例中,通过指定边集合关系来使用匿名图。 You can also use a named graph, like
GRAPH "yourGraph"
.您还可以使用命名图,例如
GRAPH "yourGraph"
。
Starting at nodes/2699394
, the edges down to nodes/2699418
are traversed and every node on the way is returned unchanged so far.从
nodes/2699394
开始,向下到nodes/2699418
的边被遍历,并且途中的每个节点到目前为止都没有改变。
Since we are only interested in the x
attribute, we can change that to only return that attribute: RETURN vx
- which will return [ 5, 3, 2, 5 ]
.由于我们只对
x
属性感兴趣,我们可以将其更改为仅返回该属性: RETURN vx
- 这将返回[ 5, 3, 2, 5 ]
。 Unless we say IN 0..10
, the start vertex will not be included.除非我们说
IN 0..10
,否则不会包括起始顶点。
Inside the FOR loop, we don't have access to all the x
values, but only one at a time.在 FOR 循环中,我们无法访问所有
x
值,但一次只能访问一个。 We can't do something like RETURN SUM(vx)
here.我们不能在这里做类似
RETURN SUM(vx)
事情。 Instead, we need to assign the result of the traversal to a variable, which makes it a sub-query.相反,我们需要将遍历的结果分配给一个变量,这使它成为一个子查询。 We can then add up all the numbers and return the resulting value:
然后我们可以将所有数字相加并返回结果值:
LET x = (
FOR v IN 1..10 INBOUND "nodes/2699394" relations
RETURN v.x
)
RETURN SUM(x) // [ 15 ]
If you want to return the start node with a computed x
attribute, you may do the following:如果要返回具有计算
x
属性的起始节点,可以执行以下操作:
LET doc = DOCUMENT("nodes/2699394")
LET x = (
FOR v IN 1..10 INBOUND doc relations
RETURN v.x
)
RETURN MERGE( doc, { x: SUM(x) } )
The result will be like:结果将是这样的:
[
{
"_id": "nodes/2699394",
"_key": "2699394",
"_rev": "2699394",
"x": 15
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.