gpt4 book ai didi

arangodb - 使用 AQL 变量,例如用于计数(LET sum = sum + 1)

转载 作者:行者123 更新时间:2023-12-02 03:29:36 25 4
gpt4 key购买 nike

根据 https://www.arangodb.com/2014/07/13/arangodb-2-2-0-released应该可以使用这样的语句:

LET sum = 0
FOR v IN values
SORT v.year
LET sum = sum + v.value
RETURN { year: v.year, value: v.value, sum: sum }

我目前使用 2.4 版,但无法使用它,例如在这样的声明中:
LET sum = 0
FOR i in memoryColl
LET sum = sum + 1
// sum = sum + 1
RETURN { "i": i, "sum": sum }

我得到了错误
[1511] 变量 'sum' 被分配多次(在解析时)

有人能告诉我这样的statemtn原则上是否应该有效,以及具体如何?

最佳答案

upgrading docs 中所述对于 2.3,不再可能更新查询中的变量:

Previous versions of ArangoDB allowed the modification of variables inside AQL queries [...]

While this is admittedly a convenient feature, the new query optimizer design did not allow to keep it.

Additionally, updating variables inside a query would prevent a lot of optimizations to queries that we would like the optimizer to make. Additionally, updating variables in queries that run on different nodes in a cluster would like cause non-deterministic behavior because queries are not executed linearly.



要枚举文档,你可以这样做
LET range = 0..LENGTH(memoryColl)-1

FOR i IN range
RETURN {i: i+1, doc: memoryColl[i]}

但对我来说这似乎是一个非常糟糕的主意。最好返回文档并让客户枚举它们。

如果你真的想计算文档的数量,你可以使用子查询:
LET result = (
FOR doc IN memoryColl
FILTER True // add some condition here for instance
RETURN doc
)

RETURN LENGTH(result)

在2.4中,还可以更高效地计数:
http://jsteemann.github.io/blog/2014/12/12/aql-improvements-for-24/

关于arangodb - 使用 AQL 变量,例如用于计数(LET sum = sum + 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27890497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com