什么是使用IF THEN在AQL的正确方法?

问题描述 投票:0回答:1

我想如果那么风格AQL使用,但唯一的相关操作员,我能找到的AQL文档中是三元运算符。我尝试添加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

提供了以下错误信息:

stacktrace: ArangoError: AQL: syntax error, unexpected identifier near 'doc == null || now - doc.last >=...' at position 1:51 (while parsing)
condition arangodb upsert aql arangojs
1个回答
2
投票

三元运算符不能用像一个if / else语句的方式构建试了试。它是有条件的(子)这样的表达式使用计算mult。它不能自立,有什么就可以归还或分配,如果你写它就像if-表达。

此外,它需要大括号,但实际问题是,体内含有像LETUPSERTRETURN操作。这些语言结构不能表达的内部使用。

如果我理解正确的话,你希望:

  • 如果与主要a-b没有文档中收集xb还不存在插入一个新的文档
  • 如果它确实存在,那么更新它,但只有最后一次更新为45秒或更长时间前

为你做下面的查询工作?

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

我加_keyINSERT,否则文件将得到一个自动生成的密钥,这似乎并不打算。使用FOR回路和FILTER作用就像IF构建体(无ELSE)。因为这是一个数据修改查询时,它是没有必要明确RETURN任何东西,在你的原始查询您RETURN null的ELSE情况下反正。虽然你会导致[ null ],矿山生产[ ](真正空的结果),如果你试图执行快速连续并没有什么查询被更新或插入。

需要注意的是必须使用PARSE_IDENTIFIER()摆脱文档ID字符串键和做INSERT { _key: key }。随着INSERT { _key: doc._key }你会碰上在插入时的无效文档关键错误,因为如果没有文件xp/a-bDOCUMENT()返回null因此doc._keynull,导致_key: null - 这是无效的。

© www.soinside.com 2019 - 2024. All rights reserved.