Collect doesnt return intended results

Hi all,
I am trying to get the size of function collect so that I can use it later as a condition for apoc.do.case but I return (1,1,1). I understand it has to do with the WITH clause but im not sure why this is the case.

UNWIND [{name:'test1'},{name:'test2'},{name:'test3'}] AS row1
UNWIND [{Type:'Sports car'}] AS row2
MATCH (c:Car {name:row1.name})
SET c += row2
WITH collect(row1) as combine, c
RETURN size(combine)

Intended results is size(combine) = 3
Thanks in advance.

Hello @tarendran.vivekanand :slight_smile:

You can get the size of your collection before to UNWIND :slight_smile:
Are you using parameters for your request and if yes, could you show us its version?

Moreover what is the a in the WITH? Did you try to remove it?

Regards,
Cobra

Hi @Cobra :slight_smile:

My query is as such as im going to perform an apoc.do.when later (shown below).
Thus, even if I place the size before I have the same problem.

UNWIND [{name:'test1'},{name:'test2'},{name:'test3'}] AS row1
UNWIND [{Type:'Sports car'}] AS row2
MATCH (c:Car {name:row1.name})
SET c += row2
WITH collect(row1) as combine, c
CALL apoc.do.when(
    size(combine) > 1,
    'MATCH (d:Driver {name:"User1"})
    MERGE (d)-[:USES]->(c)
    RETURN c',
    'RETURN NULL',
    {c:c,combine:combine}) YIELD value
RETURN value

What is a in WITH collect(row1) as combine, a?

sorry meant to use c, edited my comment

What does return

UNWIND [{name:'test1'},{name:'test2'},{name:'test3'}] AS row1
UNWIND [{Type:'Sports car'}] AS row2
MATCH (c:Car {name:row1.name})
SET c += row2
RETURN collect(row1) as combine, c

It returns combine by separating it into 3

So your collect is useless, it's because of the c, if you don't return the c, it should be only one list. Are you using arguements in your request? Because you could directly return the size of the argument:)

Yes i am using c later in the apoc.do.when. Even if I place the match inside the apoc.do.when, I cant use the collect function in the apoc.do.when: size(collect(row1)) :(

No I mean do you use something like:

UNWIND $row1 AS row1
UNWIND $row2 AS row2
1 Like

I finally got what u meant
Thanks @Cobra :slight_smile:

UNWIND $test AS row1
UNWIND [{Type:'Sports car'}] AS row2
MATCH (c:Car {name:row1.name})
SET c += row2
WITH c
CALL apoc.do.when(
   size($test) > 1,
   'MATCH (d:Driver {name:"User1"})
   MERGE (d)-[:USES]->(c)
   RETURN c',
   'RETURN NULL',
   {c:c,test:$test}) YIELD value
RETURN value
1 Like

Does it works? :slight_smile:

Yea when i used the above query. Thanks :slight_smile:

1 Like