[英]What is the proper way to use IF THEN in AQL?
I'm trying to use IF THEN style AQL, but the only relevant operator I could find in the AQL documentation was the ternary operator. 我正在尝试使用IF THEN样式的AQL,但是我可以在AQL文档中找到的唯一相关运算符是三元运算符。 I tried to add IF THEN syntax to my already working AQL but it gives syntax errors no matter what I try.
我试图将IF THEN语法添加到我已经可以使用的AQL中,但是无论我如何尝试,它都会产生语法错误。
LET doc = DOCUMENT('xp/a-b')
LET now = DATE_NOW()
doc == null || now - doc.last >= 45e3 ?
LET mult = (doc == null || now - doc.last >= 6e5 ? 1 : doc.multiplier)
LET gained = FLOOR((RAND() * 3 + 3) * mult)
UPSERT {_key: 'a-b'}
INSERT {
amount: gained,
total: gained,
multiplier: 1.1,
last: now
}
UPDATE {
amount: doc.amount + gained,
total: doc.total + gained,
multiplier: (mult < 4 ? FLOOR((mult + 0.1) * 10) / 10 : 4),
last: now
}
IN xp
RETURN NEW
:
RETURN null
Gives the following error message: 给出以下错误信息:
stacktrace: ArangoError: AQL: syntax error, unexpected identifier near 'doc == null || now - doc.last >=...' at position 1:51 (while parsing)
The ternary operator can not be used like an if/else construct in the way to tried. 在尝试的方式中,不能像if / else构造一样使用三元运算符。 It is for conditional (sub-)expressions like you use to calculate
mult
. 它用于条件(子)表达式,就像您用来计算
mult
。 It can not stand by itself, there is nothing it can be returned or assigned to if you write it like an if-expression. 它本身不能独立存在,如果您像if-expression一样编写它,则无法将其返回或分配给它。
Moreover, it would require braces, but the actual problem is that the body contains operations like LET
, UPSERT
and RETURN
. 而且,这需要大括号,但是实际的问题是主体包含诸如
LET
, UPSERT
和RETURN
。 These are language constructs which can not be used inside of expressions. 这些是不能在表达式内部使用的语言构造。
If I understand correctly, you want to: 如果我理解正确,则您希望:
ab
exists yet in collection xb
xb
不存在带有键ab
文档,则插入一个新文档 Does the following query work for you? 以下查询对您有用吗?
FOR id IN [ 'xp/a-b' ]
LET doc = DOCUMENT(id)
LET key = PARSE_IDENTIFIER(id).key
LET now = DATE_NOW()
FILTER doc == null || now - doc.last >= 45e3
LET mult = (doc == null || now - doc.last >= 6e5 ? 1 : doc.multiplier)
LET gained = FLOOR((RAND() * 3 + 3) * mult)
UPSERT { _key: key }
INSERT {
_key: key,
amount: gained,
total: gained,
multiplier: 1.1,
last: now
}
UPDATE {
amount: doc.amount + gained,
total: doc.total + gained,
multiplier: (mult < 4 ? FLOOR((mult + 0.1) * 10) / 10 : 4),
last: now
}
IN xp
RETURN NEW
I added _key
to INSERT
, otherwise the document will get an auto-generated key, which does not seem intended. 我在
INSERT
添加了_key
,否则文档将获得一个自动生成的密钥,这似乎不是故意的。 Using a FOR
loop and a FILTER
acts like an IF construct (without ELSE). 使用
FOR
循环和FILTER
行为就像是IF构造(没有ELSE)。 Because this is a data modification query, it is not necessary to explicitly RETURN
anything and in your original query you RETURN null
for the ELSE case anyway. 由于这是一个数据修改查询,因此无需显式
RETURN
任何内容,并且在原始查询中,无论如何对于ELSE情况都RETURN null
。 While yours would result in [ null ]
, mine produces [ ]
(truly empty result) if you try execute the query in quick succession and nothing gets updated or inserted. 当您的查询结果为
[ null ]
,如果您尝试快速连续执行查询并且没有任何内容被更新或插入,则我的将产生[ ]
(真正为空的结果)。
Note that it is necessary to use PARSE_IDENTIFIER()
to get the key from the document ID string and do INSERT { _key: key }
. 请注意,有必要使用
PARSE_IDENTIFIER()
从文档ID字符串获取密钥并执行INSERT { _key: key }
。 With INSERT { _key: doc._key }
you would run into an invalid document key error in the insert case, because if there is no document xp/ab
, DOCUMENT()
returns null
and doc._key
is therefore also null
, leading to _key: null
- which is invalid. 使用
INSERT { _key: doc._key }
您会在插入情况下遇到无效的文档密钥错误,因为如果没有文档xp/ab
, DOCUMENT()
返回null
,因此doc._key
也为null
,导致_key: null
无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.