Showing posts with label Neo4j. Show all posts
Showing posts with label Neo4j. Show all posts

Saturday, 18 September 2021

Neo4j commands cheat sheet

Command

Syntax

Example or Description

Create node

CREATE (node_name)

CREATE(my_node1)

Create multiple nodes in same query

CREATE (node_name_1), (node_name_2) …. (node_name_N)

CREATE(my_node2), (my_node3)

Create node with label

CREATE (node_name:label)

CREATE (my_node4:demoNode4)

Create a node with multiple labels

CREATE (node_name:label1:label2:. . . . labelN)

CREATE (my_node5:demoNode5:exampleNode5:helloNode5)

Create a node with properties

CREATE (node_name:label { key1: value, key2: value, . . . . . . . . .  })

CREATE (Ram:Person{name: “Rama Krishna”, age: 32, city: “Bangalore”})

Return the created node after creation

CREATE (node_name:label{properties. . . . }) RETURN node_name

CREATE (Sailaja:Person{name: “Sailaja”, age: 32, city: “Hyderabad”, female: true}) RETURN Sailaja

Return multiple nodes after creation

CREATE (node1) (node2) RETURN node1, node2

CREATE (RamaKrishna:Person{name: “Rama Krishna”, age: 32, city: “Bangalore”, male: true}), (MuraliKrishna:Person{name: “Murali Krishna”, age: 34, city: “Hyderabad”, male: true}) RETURN RamaKrishna, MuraliKrishna

Create relationship

CREATE (NODE_1)-[:RELATIONSHIP_TYPE]->(NODE_2)

CREATE (RamaKrishna:Person{name: “Rama Krishna”, age: 32, city: “Bangalore”, male: true})

CREATE (Suhana:Person{name: “Suhana Krishna”, age: 2, city: “Bangalore”, female: true})

CREATE (RamaKrishna)-[:FATHER_OF]->(Suhana)

Create self-relationship

CREATE (node)-[:Relationship]->(node)

CREATE (elephant:Animal{name: “Ganesha”})-[:BIG_ANIMAL]->(elephant)

 

Add label, properties to a relationship

CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-> (node2)

 

CREATE (RamaKrishna:Person{name: “Rama Krishna”, age: 32, city: “Bangalore”, male: true})

CREATE (MuraliKrishna:Person{name: “Murali Krishna”, age: 34, city: “Hyderabad”, male: true})

CREATE (RamaKrishna)-[c:COLLEAGUES {since: “5 yrs”, organization: “yahoo”}]->(MuraliKrishna)

 

Get all the nodes

MATCH (n) RETURN n

 

Delete all the nodes

MATCH (n) DETACH DELETE (n)

 

Get the node with given id

MATCH (n) WHERE id(n) = {NODE_ID} RETURN n

MATCH (n) WHERE id(n) = 4 RETURN n

 

Limit number of items

MATCH (n) RETURN n LIMIT NUMBER_OF_ITEMS

 

MATCH (n) RETURN n LIMIT 5

Match nodes by label and properties

MATCH (n:label{property1: value1, property2: value2….propertyN: valueN}) RETURN n

 

Get all the nodes with label Location

MATCH (n:Location) RETURN n

 

Get all the nodes which are labelled with Person and whose designation is Manager

MATCH (n:Person{designation: “Manager”}) RETURN n

Match the nodes that has any relationship with other nodes

MATCH (n1)—(n2) RETURN n1, n2

 

Get nodes and their relationships

MATCH (n1)-[rel]-(n2) RETURN n1, rel, n2

 

 

Specify  relationship direction while querying

MATCH (n1)-[rel]->(n2) RETURN n1, rel, n2

Above statement return the nodes and relationship, if there is any relationship from n1 to n2.

 

MATCH (n1)<-[rel]-(n2) RETURN n1, rel, n2

Above statement return the nodes and relationship, if there is any relationship from n2 to n1.

 

Specify relationship type and properties while querying

MATCH (n1:label{property1: value1, property2: value2….propertyN: valueN})-[rel:relationShipType{property1: value1, property2: value2….propertyN: valueN}]->(n2:label{property1: value1, property2: value2….propertyN: valueN}) RETURN n1, rel, n2

MATCH (n1)-[rel:REPORT_TO{from: 1997}]->(n2) RETURN n1, rel, n2

Specify multiple relationship types using |

MATCH (n1)-[rel:RELATIVE_TO | FRIEND_TO]->(n2) RETURN n1, n2, rel

 

Matches the nodes if there is any relationship RELATIVE_TO or FRIEND_TO between nodes from n1 to n2.

Specify the properties to return

MATCH (n1)-[rel]->(n2) RETURN n1.propertyName, rel. propertyName, n2.propertyName

MATCH (n1)-[rel]->(n2) RETURN n1.name, rel.description, n2.name, rel.from

 

Extract specific properties from query result

MATCH (n1)-[rel]->(n2) RETURN n1.propertyName, rel. propertyName, n2.propertyName

 

Get all the person names

MATCH (n:Person) RETURN n.name

 

Get person names and their relationship details.

MATCH (n1)-[rel]->(n2) RETURN n1.name, rel.description, n2.name, rel.from

Filter data by properties

 

 

Get all the persons who has property age with value 32

MATCH(n:Person{age:32}) RETURN n

 

Get all the person names who are reporting to Rama Krishna

MATCH(n1:Person{name:”Rama Krishna”})<-[:REPORT_TO]->(n2) RETURN n2.name

 

Get all the persons whose designation is Manager

MATCH (n:Person{designation:”Manager”}) Return n

 

Get all the person name, age who are reporting to CEO directly

MATCH (n1)-[:REPORT_TO]->(n2:Person{designation:”CEO”}) Return n1.name, n1.age

 

Filter the data using WHERE clause

 

Get all the persons who has property age with value 32

MATCH(n:Person) WHERE n.age=32 RETURN n

 

Get all the person names who are reporting to Rama Krishna

MATCH(n1:Person)<-[:REPORT_TO]->(n2) WHERE n1.name=”Rama Krishna” RETURN n2.name

 

Get all the persons whose designation is Manager

MATCH (n:Person) WHERE n.designation=”Manager” Return n

 

Get all the person name, age who are reporting to CEO directly

MATCH (n1)-[:REPORT_TO]->(n2:Person) WHERE n2.designation=”CEO” Return n1.name, n1.age

 

Comparison operators

 

Get all the persons whose age is equal to 32

MATCH(n:Person) WHERE n.age=32 RETURN n

 

Get all the persons whose age is not equal to 32.

MATCH(n:Person) WHERE n.age<>32 RETURN n

 

Get all the persons whose age is < 32.

MATCH(n:Person) WHERE n.age<32 RETURN n

 

Get all the persons whose age is > 32

MATCH(n:Person) WHERE n.age>32 RETURN n

 

Get all the persons whose age is <= 32.

MATCH(n:Person) WHERE n.age<=32 RETURN n

 

Get all the persons whose age is >= 32.

MATCH(n:Person) WHERE n.age>=32 RETURN n

Boolean OR

 

Get all the persons whose city is Bangalore and designation is Employee

MATCH(n:Person) WHERE n.city=”Bangalore” AND n.designation=”Employee” RETURN n

Boolean AND

 

Get all the employees whose age is 32 or designation is Manager

MATCH(n:Person) WHERE n.age=32 OR n.designation=”Manager” RETURN n

IN Operator

 

Get all the persons whose age is either 32 or 45 or 38.

MATCH(n:Person) WHERE n.age IN [32, 45, 38] RETURN n

NOT Operator

 

Get all the persons whose city is not Bangalore and designation is not Employee

MATCH(n:Person) WHERE NOT (n.city=”Bangalore” OR n.designation=”Employee”) RETURN n

 

Get all the persons whose age is is not 32, 45, 38.

MATCH(n:Person) WHERE n.age NOT IN [32, 45, 38] RETURN n

ORDER BY Caluse

 

Get the person details by their salary in ascending order.

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary

 

Get the person details by their salary in descending order

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary DESC

 

You can even use ORDER BY clause on multiple properties

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary DESC, p.age

SKIP

 

Get person details who is earning second highest salary

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary DESC SKIP 1 LIMIT 1

 

Get third oldest person details

MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.age DESC SKIP 2 LIMIT 1

AS Clause: To rename properties

 

MATCH (p:Person) RETURN p.name AS My_Name, p.salary AS My_Salary, p.age AS My_Age ORDER BY p.age

DISTINCT

 

Get all the distinct cities where persons are living

MATCH (p:Person) RETURN DISTINCT(p.city) AS My_City

 

Get all the distinct designation available in the organization.

MATCH (p:Person) RETURN DISTINCT(p.designation) AS designations

COUNT

 

Count number of managers in the rganization

MATCH (n:Person{designation:”Manager”}) RETURN COUNT(n)

 

Count number of employees who are reporting to Manager directly.

MATCH (n1:Person{designation:”Manager”})<-[:REPORT_TO]-(n2:Person{designation:”Employee”}) RETURN COUNT(n2)

 

Count number of employees who are reporting to the director “Kiran”

MATCH (n1:Person{designation:”Manager”, name:”Kiran”})<-[:REPORT_TO]-(n2:Person{designation:”Employee”}) RETURN COUNT(n2)

SUM

 

Get sum of slaries of all employees

MATCH(n:Person) RETURN SUM(n.salary)

 

Get sum of salaries of employee by their designation

MATCH(n:Person) RETURN n.designation, SUM(n.salary) ORDER BY n.designation

AVG

 

Get average of slaries of all employees

MATCH(n:Person) RETURN AVG(n.salary)

 

Get average salaries of employee by their designation

MATCH(n:Person) RETURN n.designation, AVG(n.salary) ORDER BY n.designation

MIN

 

Get Minimum salary of all employees

MATCH(n:Person) RETURN MIN(n.salary)

 

Get Minimum salaries of employee by their designation

MATCH(n:Person) RETURN n.designation, MIN(n.salary) ORDER BY n.designation

MAX

 

Get Maximum salary of all employees

MATCH(n:Person) RETURN MAX(n.salary)

 

Get Maximum salaries of employee by their designation

MATCH(n:Person) RETURN n.designation, MAX(n.salary) ORDER BY n.designation

 

Update Operations

Command

Syntax

Example or Description

SET

Syntax to update properties

MATCH ….

WHERE ….

SET propertyName1 = value, propertyName2 = value2

RETURN ….

 

Syntax to add new label to a node

MATCH ….

WHERE ….

SET nodeVariable:label1:label2:…labelN

RETURN ….

Update the person name to Kiran Kumar, if his name is Kiran

MATCH (p:Person)

WHERE p.name=”Kiran”

SET p.name=”Kiran Kumar”

RETURN p

 

Add new label ‘EngineeringManager’ to all the managers.

MATCH(p:Person{designation:”Manager”})

SET p:EngineeringManager

RETURN p

 

You can even add multiple labels in single query. For example, below statement add labels M1, M2.

MATCH(p:Person{designation:”Manager”})

SET p:M1:M2

RETURN p

REMOVE

Remove a property from node

MATCH ….

WHERE ….

REMOVE variable.propertyName

RETURN ….

 

Remove a label from node

MATCH ….

WHERE ….

REMOVE variable:labelName

RETURN ….

Remove a property city from the person with name ‘Brahmam’

MATCH (p:Person)

WHERE p.name=”Brahmam”

REMOVE p.city

RETURN p

 

Remove label Handsome from the person with name ‘Kiran’ and designation ‘Director’.

MATCH (p:Person)

WHERE p.name=”Kiran” AND p.designation=”Director”

REMOVE p:Handsome

RETURN p

 

 

Delete Operations

Command

Syntax

Example or Description

Delete all the nodes

MATCH (n) DETACH DELETE (n)

 

Delete specific nodes

MATCH (node:label {properties . . . . . . . . . .  })

DETACH DELETE node

 

  (or)

MATCH (node:label)

WHERE .....

DETACH DELETE node

Delete all the persons whose designation is Manager

MATCH(n:Person{designation:"Manager"})

DETACH DELETE n

 

Delete all the persons whose designation is Director

MATCH(n:Person)

WHERE n.designation="Director"

DETACH DELETE n

 

 

Previous                                                    Next                                                    Home

Wednesday, 15 September 2021

Neo4j: Delete a node

Following syntax is used to delete a node.

 

Syntax

MATCH (node:label {properties . . . . . . . . . .  }) 
DETACH DELETE node

  (or)
MATCH (node:label)
WHERE .....
DETACH DELETE node

 

Let’s experiment with an example.

 

Step 1: Open neo4j browser by hitting following url in browser.

http://localhost:7474/browser/

 

 


 

Step 2: Insert data by executing below query.

CREATE (p1:Person{name: "Rama Krishna", age: 32, city: "Bangalore", male: true, designation: "CEO", salary:540000000}),
(p2:Person{name: "Murali", age: 32, city: "Hyderabad", male: true, designation: "Vice President", salary: 43000000}),
(p3:Person{name: "Gopi", age: 34, city: "Chennai", male: true, designation: "Vice President", salary: 63067000}),
(p4:Person:Handsome:Tall{name: "Kiran", age: 28, city: "Bangalore", male: true, designation: "Director", salary: 12000000}),
(p5:Person{name: "Siva", age: 45, city: "Hyderabad", male: true, designation: "Director", salary: 23000000}),
(p6:Person{name: "Kiran", age: 36, city: "Hyderabad", male: true, designation: "Manager", salary: 7800000}),
(p7:Person{name: "Ritweek", age: 23, city: "Bangalore", male: true, designation: "Manager", salary: 8100000}),
(p8:Person{name: "Sudheer", age: 24, city: "Chennai", male: true, designation: "Manager", salary: 7900000}),
(p9:Person{name: "Naveen", age: 37, city: "Bangalore", male: true, designation: "Employee", salary: 4500000}),
(p10:Person{name: "Sailaja", age: 31, city: "Hyderabad", female: true, designation: "Employee", salary: 5300000}),
(p11:Person{name: "Harika", age: 36, city: "Bangalore", female: true, designation: "Employee", salary: 3400000}),
(p12:Person{name: "Brahmam", age: 41, city: "Bangalore", male: true, designation: "Employee", salary: 4560000}),
(office1:Location{name: "Pluto", city: "Bangalore"}),
(office2:Location{name: "Mars", city: "Chennai"})
MERGE (p2)-[:REPORT_TO{from: 2014, description: "reports to"}]->(p1)
MERGE (p3)-[:REPORT_TO{from: 2016, description: "reports to"}]->(p1)
MERGE (p4)-[:REPORT_TO{from: 1997, description: "reports to"}]->(p2)
MERGE (p5)-[:REPORT_TO{from: 1997, description: "reports to"}]->(p3)
MERGE (p6)-[:REPORT_TO{from: 2001, description: "reports to"}]->(p4)
MERGE (p7)-[:REPORT_TO{from: 2009, description: "reports to"}]->(p4)
MERGE (p8)-[:REPORT_TO{from: 1998, description: "reports to"}]->(p5)
MERGE (p9)-[:REPORT_TO{from: 2003, description: "reports to"}]->(p6)
MERGE (p10)-[:REPORT_TO{from: 2004, description: "reports to"}]->(p6)
MERGE (p11)-[:REPORT_TO{from: 2008, description: "reports to"}]->(p7)
MERGE (p12)-[:REPORT_TO{from: 2019, description: "reports to"}]->(p8)
MERGE (p2)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p1)
MERGE (p10)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p8)
MERGE (p7)-[:FRIEND_TO{from: 2014, description: "friend to"}]->(p9)
MERGE (p3)-[:RELATIVE_TO{from: 2014, description: "relative to"}]->(p4)
RETURN p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, office1, office2




Delete all the persons whose designation is Manager

MATCH(n:Person{designation:"Manager"})
DETACH DELETE n





Delete all the persons whose designation is Director

MATCH(n:Person)
WHERE n.designation="Director"
DETACH DELETE n

 




Note

To delete a node in Neo4j, we need to make sure that there is no relationship associated with this node.

Previous                                                    Next                                                    Home