How to user the UNWIND with WHERE clause?


(Thiagovulcao) #1

Hi guys! I need some help with a operation in Neo4j.

I have a relation like (c)-[a]->(u)-[v]->(p).

Actualy i have a query return return c, u, COLLECT(v), p ... I made this logical to isolate [v] (Visit) and preserve a perspective to single result for c, u and p, and get a set of [v].

BUT, in this same query, I need to put some restrictions with WHERE using the COLLECT(v).

My doubt is:

  1. Do I have a way to use de UNWIND with a WHERE clause to iterate this collection?
  2. There's another way to organize this query?

Thank's guys!

(Andrew Bowman) #2

If you need to filter the v relationships, do so before the collection.

WITH DISTINCT c, u, v, p
WHERE <predicate to filter v relationships>
WITH c, u, p, collect(v) as rels

Otherwise, you can collect and apply a filter() operation (though I prefer list comprehension):

WITH c, u, p, collect(DISTINCT v) as rels
WITH c, u, p, [rel in rels WHERE <predicate for rel>] as rels

(Thiagovulcao) #3

Thank's Andrew, I forgot to mention the need to use a restriction by SIZE, using your example:

WHERE rels = [some integer value]
WITH c, u, p, collect(DISTINCT v) as rels

I know that query structure will never work, but that's the dilemma.

Otherwise, thanks Andrew, if you have another tip I apreciate!


(Thiagovulcao) #4

Andrew, thanks buddy you solved my problem. I use the second option that you gaved me.

WITH c, u, p, [rel in rels WHERE &lt;predicate for rel&gt;] as rels
It works fine.

Thank you so much!!