简体   繁体   中英

Query: AQL: variable '$OLD' is assigned multiple times

I want to update the following data in a nested loop. While doing this, I need to get data from the upper loops. I sent the data by cutting it a bit. There are more nested arrays. When I try to fetch data from the parent loop I get the following error. I've tried many variations, for example when I just try to RETURN it works fine but it won't let me use it in UPSERT. so like this

DATA:

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

WORKS FINE:

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
                }

Query: AQL: variable '$OLD' is assigned multiple times (while parsing):

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

As I said, there are more arrays nested inside each other. How can I solve this?

You can put the first UPSERT in a subquery like this:

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

This way the scope of the implicit $OLD variable introduced by the first UPSERT is limited to that subquery and therefore does not conflict with the second UPSERT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM