ArangoDB 查询:AQL:变量'$OLD'被多次赋值

af7jpaap  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(239)

我想在嵌套循环中更新以下数据。在执行此操作时,我需要从上层循环中获取数据。我通过将数据剪切一点来发送数据。有更多嵌套数组。当我试图从父循环中获取数据时,我得到以下错误。我尝试了许多变体,例如,当我只尝试返回时,它工作正常,但它不允许我在UPSERT中使用它。因此如下
数据:

[
  {
    "I": 28,
    "C": [
      {
        "I": 50,
        "L": [
          {
            "I": 1783,
            "E": [
              {
                "I": 5107194
              },
              {
                "I": 5184134
              },
              {
                "I": 5030548
              },
              {
                "I": 5069351
              }
            ]
          }
        ]
      }
    ]
  }
]

工作良好:

FOR sport IN ${data}
    FOR country IN sport.C
        FOR league IN country.L
            UPSERT { I: league.I } 
            INSERT { I: league.I, C: country.I, S: sport.I } 
            UPDATE { I: league.I, C: country.I, S: sport.I } IN leagues
            
            FOR event IN league.E
                RETURN {
                    I: event.I,
                    L: league.I
                }

查询:AQL:变量'$OLD'被多次赋值(解析时):

FOR sport IN ${data}
    FOR country IN sport.C
        FOR league IN country.L
            UPSERT { I: league.I } 
            INSERT { I: league.I, C: country.I, S: sport.I } 
            UPDATE { I: league.I, C: country.I, S: sport.I } IN leagues
            
            FOR event IN league.E
                UPSERT { I: event.I }
                INSERT {
                    I: event.I,
                    L: league.I
                }
                UPDATE { 
                    I: event.I,
                    L: league.I
                } IN events

正如我所说的,有更多的数组嵌套在彼此的内部。我该如何解决这个问题?

kzipqqlq

kzipqqlq1#

您可以将第一个UPSERT放在子查询中,如下所示:

FOR sport IN ${data}
    FOR country IN sport.C
        FOR league IN country.L
            LET dummy = (
              UPSERT { I: league.I } 
              INSERT { I: league.I, C: country.I, S: sport.I } 
              UPDATE { I: league.I, C: country.I, S: sport.I } IN leagues
            )
            
            FOR event IN league.E
                UPSERT { I: event.I }
                INSERT {
                    I: event.I,
                    L: league.I
                }
                UPDATE { 
                    I: event.I,
                    L: league.I
                } IN events

这样,由第一个UPSERT引入的隐式$OLD变量的作用域将限于该子查询,因此不会与第二个UPSERT冲突。

相关问题