Neo4j - Read Clause

1. Match Clause

1.1 Get All Nodes Using Match

Using the MATCH clause of Neo4j you can retrieve all nodes in the Neo4j database.
eg.
Before proceeding with the example, create 3 nodes and 2 relationships as shown below.

CREATE (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) 
CREATE (Ind:Country {name: "India", result: "Winners"}) 
CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"}) 
CREATE (Ind)-[r1:WINNERS_OF {NRR:0.938 ,pts:6}]->(CT2013) 

CREATE(Dhoni)-[r2:CAPTAIN_OF]->(Ind)  
CREATE (Dhawan:player{name: "shikar Dhawan", YOB: 1995, POB: "Delhi"}) 
CREATE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})  

CREATE (Dhawan)-[:TOP_SCORER_OF {Runs:363}]->(Ind) 
CREATE (Jadeja)-[:HIGHEST_WICKET_TAKER_OF {Wickets:12}]->(Ind)
Match Result

1.2 Getting All Nodes Under a Specific Label

MATCH (node:label) 
RETURN node 

eg
Following is a sample Cypher Query, which returns all the nodes in the database under the label player.

MATCH (n:player) 
RETURN n 

1.3 Match by Relationship

You can retrieve nodes based on relationship using the MATCH clause.

MATCH (node:label)<-[: Relationship]-(n) 
RETURN n 

eg

MATCH (Ind:Country {name: "India", result: "Winners"})<-[: TOP_SCORER_OF]-(n) 
RETURN n.name 
image.png

2. Option Match Clause

The OPTIONAL MATCH clause is used to search for the pattern described in it, while using nulls for missing parts of the pattern.
OPTIONAL MATCH is similar to the match clause, the only difference being it returns null as a result of the missing parts of the pattern.

MATCH (node:label {properties. . . . . . . . . . . . . .}) 
OPTIONAL MATCH (node)-->(x) 
RETURN x

eg.
Following is a sample Cypher Query which tries to retrieve the relations from the node ICCT2013. Since there are no such nodes, it returns null.

MATCH (a:Tornament {name: "ICC Champions Trophy 2013"}) 
OPTIONAL MATCH (a)-->(x) 
RETURN x 

Here you can observe that since there are no matches for the required pattern, Neo4j returned null.


Optional Match Result

3. Where Clause

Neo4j CQL has provided WHERE clause in CQL MATCH command to filter the results of a MATCH Query.

MATCH (label)  
WHERE label.country = "property" 
RETURN label 

eg.
Before proceeding with the example, create five nodes in the database as shown below.

CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"})
CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"})
CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222, 
   country:"Srilanka"})
CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
CREATE(Ind:Country {name: "India", result: "Winners"})

Following is a sample Cypher Query which returns all the players (nodes) that belongs to the country India using WHERE clause.

MATCH (player)  
WHERE player.country = "India" 
RETURN player 
注意,这块的截图是点了table的,不是Graph

3.1 WHERE Clause with Multiple Conditions

MATCH (player)  
WHERE player.country = "India" AND player.runs >=175 
RETURN player 
image.png

3.2 Using Relationship with Where Clause

MATCH (n) 
WHERE (n)-[: TOP_SCORER_OF]->( {name: "India", result: "Winners"}) 
RETURN n 

Here you can observe that Neo4j returned the node, which has the relation TOP_SCORER_OF to the country with the node having the name India.


image.png

4. Count

4.1 Count

The count() function is used to count the number of rows.

MATCH (n { name: 'A' })-->(x) 
RETURN n, count(*) 

eg.

Match(n{name: "India", result: "Winners"})--(x)  
RETURN n, count(*) 

4.2 Group Count

The COUNT clause is also used to count the groups of relationship types.
Following is a sample Cypher Query which counts and returns the number of nodes participating in each relation.

Match(n{name: "India", result: "Winners"})-[r]-(x)  
RETURN type (r), count(*) 
image.png

你可能感兴趣的:(Neo4j - Read Clause)