Hi,

For this particular query there are actually two more possibilities:

>> LET array = [{title: "a"},{title: "b"}]
>> FOR item IN array
>>     INSERT item INTO collection_1
>> INSERT {title: "c"} INTO collection_2

Could run as:

INSERT {title: "c"} INTO collection_2
LET array = [{title: "a"},{title: "b"}]
FOR item IN array
    INSERT item INTO collection_1

This way the second INSERT statement is bore the Loop over array.
AQL is not whitespace sensitive for scopes

Which means your query is actually:


LET array = [{title: "a"},{title: "b"}]
FOR item IN array
    INSERT item INTO collection_1
    INSERT {title: "c"} INTO collection_2

The second insert is within the loop.


The other alternative to a subquery would be a COLLECT with constant condition:

LET array = [{title: "a"},{title: "b"}]
FOR item IN array
    INSERT item INTO collection_1
COLLECT x = 1 /* This groups all outputs of the former for loop. AS the 
grouping is done on a constant, we will end up with a single Group */
INSERT {title: "c"} INTO collection_2

Best
Michael


> Am 16.05.2025 um 12:43 schrieb Wilfried Gösgens <[email protected]>:
> 
> Hi, 
> the only way AQL offers to break the scope is to use a sub-query, which will 
> then be executed before the main query. 
> 
> On Friday, May 16, 2025 at 12:41:28 PM UTC+2 [email protected] 
> <http://leiteq.nl/> wrote:
>> Hi,
>> 
>> The query below inserts the object {title: "c"} twice in collection_2.
>> 
>> However I want only one insert for the second collection.
>> 
>> The question is how to scope the FOR loop in this case? 
>> Brackets are not accepted.
>> 
>> LET array = [{title: "a"},{title: "b"}]
>> FOR item IN array
>>     INSERT item INTO collection_1
>> INSERT {title: "c"} INTO collection_2
>> 
>> Many thanks!
>> Klaas van der Molen
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "ArangoDB" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> To view this discussion visit 
> https://groups.google.com/d/msgid/arangodb/e5f06d36-b31a-461b-aa90-5bfc831c1283n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/arangodb/e5f06d36-b31a-461b-aa90-5bfc831c1283n%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"ArangoDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/arangodb/9BFEC458-DB30-4E4B-B0C4-03F13689F7CF%40arangodb.com.

Reply via email to