Instead of grouping by a constant, you can also apply a LIMIT for the same 
effect:

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

On Friday, May 16, 2025 at 1:47:45 PM UTC+2 [email protected] wrote:

> 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] 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].
> 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/2972a060-e8ff-4e54-818c-5cd00b9937e7n%40googlegroups.com.

Reply via email to