Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-11-2022 03:36 AM
Hello, I find this very hard for me to understand UNWIND, I find the UNWIND even change the value of a variable define before it(here is variable 'm').
query1:
MATCH (m:Movie)
RETURN m
resut1:
╒══════════════════════════════════════════════════════════════════════╕
│"m" │
╞══════════════════════════════════════════════════════════════════════╡
│{"tmdbId":524,"languages":["English"],"genres":["Drama","Crime"],"imdb│
│Rating":8.2,"title":"Casino","released":"1995-11-22"} │
├──────────────────────────────────────────────────────────────────────┤
│{"tmdbId":568,"languages":["English"],"genres":["Drama","Adventure","I│
│MAX"],"imdbRating":7.6,"title":"Apollo 13","released":"1995-06-30"} │
├──────────────────────────────────────────────────────────────────────┤
│{"tmdbId":858,"languages":["English"],"genres":["Comedy","Drama","Roma│
│nce"],"imdbRating":6.8,"title":"Sleepless in Seattle","released":"1993│
│-06-25"} │
├──────────────────────────────────────────────────────────────────────┤
│{"tmdbId":10410,"languages":["English","Italian","Latin"],"genres":["C│
│rime","Drama"],"imdbRating":6.6,"title":"Hoffa","released":"1992-12-25│
│"} │
└──────────────────────────────────────────────────────────────────────┘
-----------------.------------------
query2:
MATCH (m:Movie)
UNWIND m.languages as language
RETURN m
result2:
╒══════════════════════════════════════════════════════════════════════╕
│"m" │
╞══════════════════════════════════════════════════════════════════════╡
│{"tmdbId":524,"languages":["English"],"genres":["Drama","Crime"],"imdb│
│Rating":8.2,"title":"Casino","released":"1995-11-22"} │
├──────────────────────────────────────────────────────────────────────┤
│{"tmdbId":568,"languages":["English"],"genres":["Drama","Adventure","I│
│MAX"],"imdbRating":7.6,"title":"Apollo 13","released":"1995-06-30"} │
├──────────────────────────────────────────────────────────────────────┤
│{"tmdbId":858,"languages":["English"],"genres":["Comedy","Drama","Roma│
│nce"],"imdbRating":6.8,"title":"Sleepless in Seattle","released":"1993│
│-06-25"} │
├──────────────────────────────────────────────────────────────────────┤
│{"tmdbId":10410,"languages":["English","Italian","Latin"],"genres":["C│
│rime","Drama"],"imdbRating":6.6,"title":"Hoffa","released":"1992-12-25│
│"} │
├──────────────────────────────────────────────────────────────────────┤
│{"tmdbId":10410,"languages":["English","Italian","Latin"],"genres":["C│
│rime","Drama"],"imdbRating":6.6,"title":"Hoffa","released":"1992-12-25│
│"} │
├──────────────────────────────────────────────────────────────────────┤
│{"tmdbId":10410,"languages":["English","Italian","Latin"],"genres":["C│
│rime","Drama"],"imdbRating":6.6,"title":"Hoffa","released":"1992-12-25│
│"} │
└──────────────────────────────────────────────────────────────────────┘
Solved! Go to Solution.
02-11-2022 08:25 AM
The value of m has not been changed. It is being repeated with each value from the language list for the given value of m. Basically, for each row result from your match m, the collection consisting of language elements is converted from a list to result rows. Any value returned that is associated with the list being unwind will be repeated with each new row. It’s like a flat map operation. You are converting a stream of language lists into a stream of language elements. To see the result, execute the modified query below. The benefit of unwind is that the stream of unwinded elements can be further processed by cypher.
MATCH (m:Movie)
UNWIND m.languages as language
RETURN language, m
02-11-2022 04:28 AM
MATCH (m:Movie)
UNWIND m.languages as language
RETURN m
effectively says..
find me a node with label :Movie
extract property m.languages and unwind this property, i.e. iterate thru the list
for each element in the list
return/print the node
As such if a :Movie node has a property languages and it has a value of 6 items in a list, then we iterate 1 by 1 through the 6 items in the list and then reurn/print the :Movie node for each language in the list
02-11-2022 06:00 AM
Hello, thanks for your quick reply. As your answer, the m
in query2 stand for the :Movie node for each language, it's different from m
in query1 which stand for all 4 the instance model of the :Movie node.
So can I consider UNWIND has change m
value?
02-11-2022 06:13 AM
I'm not sure I understand how UNWIND changed m
. Can you provide more specifics?
02-11-2022 08:12 AM
Hi!
For helping, UNWIND clause is used to pass from a list to many records (many result lines), opposed to COLLECT() function used to pass from records to a list (single result line).
MATCH (m:Movie)
WITH COLLECT(m) AS movies // => here a list is returned (1 result row)
UNWIND movies AS movie
RETURN movie // => here one row by movie is returned
UNWIND works well with all kind of data in a list (not only nodes or reationships.
02-11-2022 08:25 AM
In your result2, you have as many result lines as languages for the same movie.
You can check this by RETURN language, m, so three movies for English and another movie three times, one row (same movie) fo each language.
02-11-2022 08:25 AM
The value of m has not been changed. It is being repeated with each value from the language list for the given value of m. Basically, for each row result from your match m, the collection consisting of language elements is converted from a list to result rows. Any value returned that is associated with the list being unwind will be repeated with each new row. It’s like a flat map operation. You are converting a stream of language lists into a stream of language elements. To see the result, execute the modified query below. The benefit of unwind is that the stream of unwinded elements can be further processed by cypher.
MATCH (m:Movie)
UNWIND m.languages as language
RETURN language, m
02-11-2022 09:49 AM
thank you
understood.
I'm good with this issue. 😉
02-15-2022 04:47 PM
Sorry for my wrong understand of MATCH -- I think MATCH (m:Movie) will run firstly, and return a Movie List. Then the UNWIND will run.
In truely, The MATCH run like a Map function.
Thanks for your clarify.
All the sessions of the conference are now available online