Lab Notes
Subject Neo4j
1. Basic Node Creation
CREATE (n:Person {name: 'Jigish', age: 33})
RETURN n
2. Creating Multiple Nodes
CREATE (n:Person {name: 'Rana', age: 31}), (m:Person {name: 'Khyati',
age: 32})
RETURN n, m
3. Creating Nodes in Bulk (Using UNWIND)
UNWIND [
{name: 'Raam', age: 35},
{name: 'Shyam', age: 29}
] AS person
CREATE (n:Person {name: person.name, age: person.age})
RETURN n
4. Create a relationship between existing nodes in Neo4j
MATCH (a:Person {name: 'Jigish'}), (b:Person {name: 'Khyati'})
CREATE (a)-[:FRIEND_OF]->(b)
Bidirectional Relationship
MATCH (a:Person {name: 'Jigish'}), (b:Person {name: 'Khyati'})
CREATE (a)-[:FRIEND_OF]->(b), (b)-[:FRIEND_OF]->(a)
Create a Relationship with Properties
MATCH (a:Person {name: 'Jigish'}), (b:Person {name: 'Khyati'})
CREATE (a)-[:FRIEND_OF {since: ‘Oct 2016’, status:’Sub
Partner’}]->(b)
Adding Multiple Relationships with Properties
MATCH (a:Person {name: 'Jigish'}), (b:Person {name: 'Khyati'})
CREATE (a)-[:FRIEND_OF {since: 2016, status: 'Sub Partner'}]->(b),
(b)-[:COLLEAGUE_OF {since: 2015}]->(a)
5. Create both nodes and a relationship between them in a single CREATE
statement
CREATE (a:Person {name: 'Jigish', age: 33})-[:FRIEND_OF]->(b:Person
{name: 'Khyati', age: 32})
6. Add a label to an existing node in Neo4j
MATCH (n:Person {name: 'Jigish'})
SET n:Employee
7. Add multiple labels in a single SET statement
MATCH (n:Person {name: 'Jigish'})
SET n:Employee:Manager
8. Add a property to an existing node in Neo4j
MATCH (n:Person {name: 'Jigish'})
SET n.email = '
[email protected]'
9. Add multiple properties in one query
MATCH (n:Person {name: 'Jigish'})
SET n.email = '
[email protected]', n.address = '123 Main St'
10. Update a node's properties and property of relationship
Update an Existing Property
MATCH (n:Person {name: 'Jigish'})
SET n.age = 34
Update Multiple Properties
MATCH (n:Person {name: 'Jigish'})
SET n.age = 34, n.email = '
[email protected]'
Update with an Expression
MATCH (n:Person {name: 'Jigish'})
SET n.age = n.age + 1
Remove a Property
MATCH (n:Person {name: 'Jigish'})
REMOVE n.email
Rename a Property
MATCH (n:Person {name: 'Jigish'})
SET n.new_email = n.email
REMOVE n.email
Update property of relationship
MATCH (a:Person {name: 'Jigish'})-[r:FRIEND_OF]->(b:Person {name:
'Khyati'})
SET r.since = 2023, r.status = 'best friends'
Update multiple relationships of the same type
MATCH (a:Person)-[r:FRIEND_OF]->(b:Person)
SET r.since = 2023
Remove a property from a relationship
MATCH (a:Person {name: 'Jigish'})-[r:FRIEND_OF]->(b:Person {name:
'Khyati'})
REMOVE r.status
11. Delete a Node
Delete a Node without Relationships
MATCH (n:Person {name: 'Jigish'})
DELETE n
Delete a Node with Relationships (Using DETACH DELETE)
MATCH (n:Person {name: 'Jigish'})
DETACH DELETE n
Delete a node that already has relationships with other nodes, but without
using DETACH DELETE
● First, match and delete all relationships that the node has.
● Then, delete the node itself.
MATCH (n:Person {name: 'Jigish'})-[r]->()
DELETE r
MATCH (n:Person {name: 'Jigish'})
DELETE n
12. Filtration of result
Filter by Property Value
MATCH (n:Person)
WHERE n.name = 'Jigish'
RETURN n
Filtering by Age Range
MATCH (n:Person)
WHERE n.age > 30 AND n.age < 40
RETURN n
Filter by Multiple Conditions
MATCH (n:Person)
WHERE n.name = 'Jigish' AND n.age = 33
RETURN n
Filter by Pattern Matching
MATCH (n:Person)
WHERE n.name CONTAINS 'Ji'
RETURN n
Filter by Relationship Properties
MATCH (a:Person)-[r:FRIEND_OF]->(b:Person)
WHERE r.since > 2020
RETURN a, r, b
Filter Using IN Clause
MATCH (n:Person)
WHERE n.name IN ['Jigish', 'Khyati']
RETURN n
Filter by Existence of Properties
MATCH (n:Person)
WHERE exists(n.email)
RETURN n
13. Delete Relationship
Delete a Specific Relationship
MATCH (a:Person {name: 'Jigish'})-[r:FRIEND_OF]->(b:Person {name:
'Khyati'})
DELETE r
Delete All Relationships of a Specific Type
MATCH ()-[r:FRIEND_OF]->()
DELETE r
Delete Multiple Relationships based on specific conditions
MATCH (a:Person)-[r:FRIEND_OF]->(b:Person)
WHERE r.since < 2020
DELETE r
14. Using MERGE to Avoid Duplicates
MERGE (n:Person {name: 'Jigish'})
ON CREATE SET n.age = 33, n.email = '
[email protected]'
ON MATCH SET n.age = 34