Skip to content

Finding Data (Match)

Now, to see the nodes we've added, the command MATCH can be used.

Finding all Nodes

To first find all the nodes in the database, the command below can be used:

Cypher
MATCH (n) RETURN n
Example Output
+--------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                  |
+--------------------------------------------------------------------------------------------------------------------+
| (:Country {name: "Finland", countryId: 1})                                                                         |
| (:Country {name: "Sweden", countryId: 2})                                                                          |
| (:Country {name: "Sweden", countryId: 2})                                                                          |
| (:Person {firstname: "Alex", city: "Helsinki", userId: 1, email: "alley@gmail.com", age: 25, lastname: "Jones"})   |
| (:Person {firstname: "Veronica", city: "Turku", userId: 2, email: "v@gmail.com", age: 52, lastname: "Paddington"}) |
| (:Person {firstname: "Veronica", city: "Turku", userId: 3, email: "veski@gmail.com", age: 52, lastname: "Skier"})  |
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"})          |
| (:Person {firstname: "Brittany", city: "Nokia", userId: 5, email: "britpop@gmail.com", age: 28, lastname: "Pop"})  |
| (:Hobby {name: "Running", hobbyId: 2})                                                                             |
+--------------------------------------------------------------------------------------------------------------------+

9 rows

Limiting the Results

To limit the amount of results coming back from the database, LIMIT can be used:

Cypher
MATCH (n) RETURN n LIMIT 3
Example Output
+--------------------------------------------+
| n                                          |
+--------------------------------------------+
| (:Country {name: "Finland", countryId: 1}) |
| (:Country {name: "Sweden", countryId: 2})  |
| (:Country {name: "Sweden", countryId: 2})  |
+--------------------------------------------+

3 rows

Ordering the Results

To sort the results in a specific order, ORDER BY can be used:

Cypher
MATCH (n) RETURN n ORDER BY n.firstname LIMIT 3
Example Output
+-------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                 |
+-------------------------------------------------------------------------------------------------------------------+
| (:Person {firstname: "Alex", city: "Helsinki", userId: 1, email: "alley@gmail.com", age: 25, lastname: "Jones"})  |
| (:Person {firstname: "Brittany", city: "Nokia", userId: 5, email: "britpop@gmail.com", age: 28, lastname: "Pop"}) |
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"})         |
+-------------------------------------------------------------------------------------------------------------------+

3 rows

Finding specific Labels

To find specific labels, this syntax can be used. In this example, all persons are retrieved:

Cypher
MATCH (n: Person) RETURN n
Example Output
+--------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                  |
+--------------------------------------------------------------------------------------------------------------------+
| (:Person {firstname: "Alex", city: "Helsinki", userId: 1, email: "alley@gmail.com", age: 25, lastname: "Jones"})   |
| (:Person {firstname: "Veronica", city: "Turku", userId: 2, email: "v@gmail.com", age: 52, lastname: "Paddington"}) |
| (:Person {firstname: "Veronica", city: "Turku", userId: 3, email: "veski@gmail.com", age: 52, lastname: "Skier"})  |
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"})          |
| (:Person {firstname: "Brittany", city: "Nokia", userId: 5, email: "britpop@gmail.com", age: 28, lastname: "Pop"})  |
+--------------------------------------------------------------------------------------------------------------------+

5 rows

Finding multiple Labels

To find all nodes that have the specified labels, we can do this:

Cypher
MATCH (n) WHERE n:Person OR n:Hobby RETURN n
Example Output
+--------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                  |
+--------------------------------------------------------------------------------------------------------------------+
| (:Person {firstname: "Alex", city: "Helsinki", userId: 1, email: "alley@gmail.com", age: 25, lastname: "Jones"})   |
| (:Person {firstname: "Veronica", city: "Turku", userId: 2, email: "v@gmail.com", age: 52, lastname: "Paddington"}) |
| (:Person {firstname: "Veronica", city: "Turku", userId: 3, email: "veski@gmail.com", age: 52, lastname: "Skier"})  |
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"})          |
| (:Person {firstname: "Brittany", city: "Nokia", userId: 5, email: "britpop@gmail.com", age: 28, lastname: "Pop"})  |
| (:Hobby {name: "Running", hobbyId: 2})                                                                             |
+--------------------------------------------------------------------------------------------------------------------+

6 rows

The example above finds all persons and hobbies.

Finding by Property

To filter the results by property, for example to find all the nodes with the label Person with lastname set to Jones, the example code below can be used:

Cypher
MATCH (n: Person { lastname: "Jones" } ) RETURN n
Example Output
+------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                |
+------------------------------------------------------------------------------------------------------------------+
| (:Person {firstname: "Alex", city: "Helsinki", userId: 1, email: "alley@gmail.com", age: 25, lastname: "Jones"}) |
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"})        |
+------------------------------------------------------------------------------------------------------------------+

2 rows

To further add more WHERE clauses, have a look at the example below:

Cypher
MATCH (n: Person) WHERE n.firstname = "Tom" OR n.firstname = "Brittany" RETURN n
Example Output
+-------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                 |
+-------------------------------------------------------------------------------------------------------------------+
| (:Person {firstname: "Tom", city: "Pori", userId: 4, age: 18, email: "tom@gmail.com", lastname: "Jones"})         |
| (:Person {firstname: "Brittany", city: "Nokia", userId: 5, email: "britpop@gmail.com", age: 28, lastname: "Pop"}) |
+-------------------------------------------------------------------------------------------------------------------+

2 rows

This finds all the persons whose name is Tom or Brittany.

Getting only specific properties back

Similar to using the SQL syntax, we can also tell what properties we want back. For example:

Cypher
MATCH (n: Person ) RETURN n.firstname, n.email
Example Output
+-----------------------------------+
| n.firstname | n.email             |
+-----------------------------------+
| "Alex"      | "alley@gmail.com"   |
| "Veronica"  | "v@gmail.com"       |
| "Veronica"  | "veski@gmail.com"   |
| "Tom"       | "tom@gmail.com"     |
| "Brittany"  | "britpop@gmail.com" |
+-----------------------------------+

5 rows

And one interesting thing is that in this case, we can also determine if the result we get back is labeled as Country or Person by adding:

Cypher
MATCH (n: Person ) RETURN n.firstname, n.email, n:Country, n:Person
Example Output
+----------------------------------------------------------+
| n.firstname | n.email             | n:Country | n:Person |
+----------------------------------------------------------+
| "Alex"      | "alley@gmail.com"   | FALSE     | TRUE     |
| "Veronica"  | "v@gmail.com"       | FALSE     | TRUE     |
| "Veronica"  | "veski@gmail.com"   | FALSE     | TRUE     |
| "Tom"       | "tom@gmail.com"     | FALSE     | TRUE     |
| "Brittany"  | "britpop@gmail.com" | FALSE     | TRUE     |
+----------------------------------------------------------+

5 rows