This website uses cookies. By clicking Accept, you consent to the use of cookies. Click Here to learn more about how we use cookies.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results forΒ

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

- Neo4j
- Technical Discussions
- Neo4j Graph Platform
- Re: What exactly happend when we UNWIND a path an...

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 10:05 AM

hi all

I need more clarity of UNWIND key word and also My issue out here is I have query like this:-

profile match p=(v:Vendor)-[*0..30]->(m:MatPlant)-[*0..30]->(d:MatPlant)-[*0..30]->(c:MatPlant)-[*0..30]->(u:Customer)

where u.node_id in ["yxz"] and v.node_id in ["xyz"] and d.node_id in ['abc]

and c.node_id in ['US000'] and m.node_id in ['00A06']

unwind (nodes(p)) as t

with collect(p) as p_list,t

optional match k=(t)-[:MfgMatPlant_To_WorkCenter]->(wc:WorkCenter)

with p_list + collect(DISTINCT k) as result_list

unwind result_list as result

return result

In the optional match command I am trying to get workcenter nodes included When Its searching, i know that workcenter will be connected to only matplant node(m,d,c) and intermediate matplant nodes in flow but not Vendor and customer how to remove these vendor node from the list

Solved! Go to Solution.

2 ACCEPTED SOLUTIONS

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 03:44 PM

The 'unwind' clause converts a list of values into rows of values. This will be done row-by-row when you have query results with multiple rows. Further, any variables associated with the row will be appended to each row created by the unwind. The following is an example.

```
with [1,2,3] as numbers, 'a' as a, 'b' as b
unwind numbers as number
return number, a, b
```

The result of the unwind is the following. Notice how the values of 'a' and 'b' are appended to each line generated from unwinding the 'numbers' list.

The 'unwind' will be done for each result row from a query, thus if you had 10 rows and each had a list of 5 elements, the unwind would result in 50 rows.

Assume in your query that you have 5 separate paths that result from the match, say p1, p2, ..., p5. Each may have a different number of nodes. The result of your 'unwind nodes(p) as t' line may look something like the following, where I have rows for each node in each path.

The result of your next line 'collect(p) as p_list, t' is to group each row by the value of 't', and collect the paths 'p' that have the same value of 't'. Recall 't' represents the nodes, so the data will be grouped into lines that have the same node and collect all the paths that that node is a part of. Remember there could be nodes common to more than one path. For illustration purposes, assuming you have 'N' distinct nodes across all paths, then you output would look something like the following:

Next, your 'optional match' will be performed for each row (distinctNode) shown in the table above. This will generate zero to 'M' paths for each distance node ('t'). If your data only allows for one potential path, then you can have either zero or one path.

The final 'with' clause is collecting all these paths for a single distinct node 't' and adding these paths to the p_list list that contains all the paths that the distinct node 't' is a member of. Assuming multiple potential paths 'k', you 'result_list' will look something like the following table:

When you finally 'unwind' result_list, you get a set of rows where each row contains one path from each of the lists in the table above. It looks like you could get duplicate paths 'p', so you should 'return distinct result' instead.

Is this what you are looking for, a list of all the paths 'p' and 'k', or do you just want all the nodes along all these paths? If it is the later, you could probably get this more efficiently with an APOC path procedure.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-02-2022 02:26 AM

Sure, you can remove them list before you unwind or after the unwind.

1. Deleting from the list:

with [x,y,z,k,l] as list

with [n in list where n<>x or n<>z] as newList

2. delete following unwind

with [x,y,z,k,l] as list

unwind list as s

with s

where s<>x or s<>z

do something on βsβ

8 REPLIES 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 10:57 AM - edited β08-01-2022 10:59 AM

Try this:

match (v:Vendor)

where v.node_id in ["xyz"]

CALL apoc.path.spanningTree(v, {labelFilter:'MatPlant|Customer', relationshipFilter:'-MfgMatPlant_To_WorkCenter', maxLevel:30}) yield path

with nodes(path) as n1, relationships(path) as r1, length(path) as lvl

unwind n1 as n2

unwind r1 as r2

with labels(n2) as lbl

unwind lbl as lbl1

return distinct lbl1

Check to see if you get WorkCenter node label

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 10:52 PM

No I couldnt able to see any data coming

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 10:57 PM - edited β08-01-2022 11:03 PM

Run this and post the result as an image:

match (v:Vendor)

where v.node_id in ["xyz"]

CALL apoc.path.spanningTree(v, {labelFilter:'MatPlant|Customer', relationshipFilter:'-MfgMatPlant_To_WorkCenter', maxLevel:6}) yield path

return path

I used the relationship and node names as shown in your post.

Run this query and post the result as image:

call apoc.meta.graph()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 11:13 PM

Run your query with small change and post the results:

match p=(v:Vendor)-[*0..30]->(m:MatPlant)-[*0..30]->(d:MatPlant)-[*0..30]->(c:MatPlant)-[*0..30]->(u:Customer)

where u.node_id in ["yxz"] and v.node_id in ["xyz"] and d.node_id in ['abc]

and c.node_id in ['US000'] and m.node_id in ['00A06']

unwind (nodes(p)) as t

return distinct labels(t) as lbl

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 11:45 PM

with collect(p) as p_list

return p_list

The result is same as return p.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 03:44 PM

The 'unwind' clause converts a list of values into rows of values. This will be done row-by-row when you have query results with multiple rows. Further, any variables associated with the row will be appended to each row created by the unwind. The following is an example.

```
with [1,2,3] as numbers, 'a' as a, 'b' as b
unwind numbers as number
return number, a, b
```

The result of the unwind is the following. Notice how the values of 'a' and 'b' are appended to each line generated from unwinding the 'numbers' list.

The 'unwind' will be done for each result row from a query, thus if you had 10 rows and each had a list of 5 elements, the unwind would result in 50 rows.

Assume in your query that you have 5 separate paths that result from the match, say p1, p2, ..., p5. Each may have a different number of nodes. The result of your 'unwind nodes(p) as t' line may look something like the following, where I have rows for each node in each path.

The result of your next line 'collect(p) as p_list, t' is to group each row by the value of 't', and collect the paths 'p' that have the same value of 't'. Recall 't' represents the nodes, so the data will be grouped into lines that have the same node and collect all the paths that that node is a part of. Remember there could be nodes common to more than one path. For illustration purposes, assuming you have 'N' distinct nodes across all paths, then you output would look something like the following:

Next, your 'optional match' will be performed for each row (distinctNode) shown in the table above. This will generate zero to 'M' paths for each distance node ('t'). If your data only allows for one potential path, then you can have either zero or one path.

The final 'with' clause is collecting all these paths for a single distinct node 't' and adding these paths to the p_list list that contains all the paths that the distinct node 't' is a member of. Assuming multiple potential paths 'k', you 'result_list' will look something like the following table:

When you finally 'unwind' result_list, you get a set of rows where each row contains one path from each of the lists in the table above. It looks like you could get duplicate paths 'p', so you should 'return distinct result' instead.

Is this what you are looking for, a list of all the paths 'p' and 'k', or do you just want all the nodes along all these paths? If it is the later, you could probably get this more efficiently with an APOC path procedure.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-01-2022 10:43 PM

Thanks understood How unwind works.

Consider we have list[x,y,z,k,l] i have unwinded into S variable it so i will get like

x

y

z

k

l

is there any way that i can delete x,z from s?and get all the details of s

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β08-02-2022 02:26 AM

Sure, you can remove them list before you unwind or after the unwind.

1. Deleting from the list:

with [x,y,z,k,l] as list

with [n in list where n<>x or n<>z] as newList

2. delete following unwind

with [x,y,z,k,l] as list

unwind list as s

with s

where s<>x or s<>z

do something on βsβ

Nodes 2022

October 19th -

October 20th -

October 25th -

October 26th -

November 2nd -

Related Content

- Does path matching with a LIMIT clause guarantee to find the closest node? in Neo4j Graph Platform
- openCypher 1.0.0-M19 in Neo4j Graph Platform
- APOC.Path.expand and MUST BE circular paths in Neo4j Graph Platform
- Deleting / Merging Duplicate Connected Nodes in Neo4j Graph Platform
- cypher to display all connections to nodes in Neo4j Graph Platform