Skip to content

UNWIND

The UNWIND statement splits a list into separated rows.

UNWIND can function as an individual statement or a clause in a statement.

Syntax

UNWIND <list> AS <alias> <RETURN clause>

Split a list

The following example splits the list [1,2,3] into three rows.

nebula) [basketballplayer]> UNWIND [1,2,3] AS n RETURN n;
+---+
| n |
+---+
| 1 |
+---+
| 2 |
+---+
| 3 |
+---+
Got 3 rows (time spent 806/2126 us)

Return a list with distinct items

Use UNWIND and WITH DISTINCT together to return a list with distinct items.

Example 1

The following statement:

  1. Splits the list [1,1,2,2,3,3] into rows.
  2. Removes duplicated rows.
  3. Sorts the rows.
  4. Transforms the rows to a list.
nebula> WITH [1,1,2,2,3,3] AS n \
        UNWIND n AS r \
        WITH DISTINCT r AS r \
        ORDER BY r \
        RETURN collect(r);
+------------+
| COLLECT(r) |
+------------+
| [1, 2, 3]  |
+------------+
Got 1 rows (time spent 307/1043 us)

Example 2

The following statement:

  1. Outputs the vertices on the matched path into a list.
  2. Splits the list into rows.
  3. Removes duplicated rows.
  4. Transforms the rows to a list.
nebula> MATCH p=(v:player{name:"Tim Duncan"})--(v2) \
        WITH nodes(p) AS n \
        UNWIND n AS r \
        WITH DISTINCT r AS r \
        RETURN collect(r);
+----------------------------------------------------------------------------------------------------------------------+
| COLLECT(r)                                                                                                           |
+----------------------------------------------------------------------------------------------------------------------+
| [("player100" :player{age: 42, name: "Tim Duncan"}), ("player101" :player{age: 36, name: "Tony Parker"}), 
("team204" :team{name: "Spurs"}), ("player102" :player{age: 33, name: "LaMarcus Aldridge"}), 
("player125" :player{age: 41, name: "Manu Ginobili"}), ("player104" :player{age: 32, name: "Marco Belinelli"}), 
("player144" :player{age: 47, name: "Shaquile O'Neal"}), ("player105" :player{age: 31, name: "Danny Green"}), 
("player113" :player{age: 29, name: "Dejounte Murray"}), ("player107" :player{age: 32, name: "Aron Baynes"}), 
("player109" :player{age: 34, name: "Tiago Splitter"}), ("player108" :player{age: 36, name: "Boris Diaw"})] |
+----------------------------------------------------------------------------------------------------------------------+
Got 1 rows (time spent 6157/6833 us)

Last update: April 13, 2021
Back to top