Neo4j operations

Neo4j is a native graph-based database language with a graph-based storage structure, using the Cypher query language to reduce code complexity and length. It excels in handling graphical data through graph traversal algorithms, making it ideal for complex queries. Neo4j is ACIDAtomicity, Consistency, Isolation, Durability-supportive, ensuring data integrity and consistency, and can scale horizontally for handling large datasets and significant queries concurrently. It is primarily used in social media applications and recommendation engines due to its speed and connection properties, enabling smoother connections between the query engine and requests.

Neo4j database structure
Neo4j database structure

Moreover, Neo4j allows multiple data querying and manipulation operations that use its Cypher language to query. We will now discuss some of them below.

Operations

In Neo4j, the data elements are stored as nodes, and edges between nodes depict relationships between data elements. This structure uses queries independently or combined to query complex data from our graph-based database.

Creating nodes and relationships

The CREATE command is used to create nodes, i.e., data elements. Then the new node is created by assigning it (variable:Label {properties}) where the variable is an optional identifier created to be referenced later. The Label is the node's label and properties defines the properties we can assign to the node. Moreover, to create a relationship between two nodes, we add a relationship tag between two nodes in the CREATE command. The syntax for this is -[r:type]-> where the - and -> dictate the direction of the relationship. The r is an optional identifier created to be referenced later, while the type is the type of relationship between the two nodes.

CREATE (person:Person {name: 'Adam'})
CREATE (person:Person {name: 'John'})-[r:LIKES]->(movie:Movie {title: 'The Matrix'})
Creating nodes and node relationships
Created nodes and node relationships
Created nodes and node relationships

Querying

The MATCH command is used to query for nodes with their labels. To find all nodes, we use the MATCH command followed by (variable:Label), which finds all nodes that fill this criteria. Then we use the RETURN keyword followed by the variable to return the found nodes.

MATCH (people:Person)
RETURN people
Querying all nodes
Queried nodes
Queried nodes

Furthermore, we can use the following syntaxes to query based on some conditions. To find nodes with specific properties, we can use the MATCH commands followed by (variable:Label {properties}). To find specific nodes and relationships or other patterns, we can use the MATCH command followed by (variable:Label {properties})-[r:type]->(variable:Label {properties}). Both of these are followed by a RETURN command to display the collected data.

MATCH (people:Person {name: 'Adam'}) RETURN people
MATCH (people:Person {name: 'John'})-[r:LIKES]->(movie:Movie {title: 'The Matrix'})
RETURN people, movie
Querying specific nodes
Queried specific nodes
Queried specific nodes

Updating nodes and relationships

The MATCH command can again be used in the context of updating. First, the command is used to find the nodes that we wish to change, and then we use the SET command followed by the properties to add or update values in our nodes.

MATCH (person:Person {name: 'John'})
SET person.age = 30
Updating property of nodes

Updated property of nodes
Updated property of nodes

Deleting nodes and relationships

Again, we use the MATCH command to find all nodes fitting our criteria. Then we can use the DETACH DELETE variable to break relationships and remove nodes.

MATCH (person:Person {name: 'John'})
DETACH DELETE person
Deleting nodes and relationships
Deleted nodes and relationships
Deleted nodes and relationships

Filtering and sorting

We can filter and sort the nodes we get from the MATCH command. To do so, we use the WHERE command to filter nodes and then RETURN command to get our values back. We can also use the ORDER BY command to set our return values in ASCENDING or DESCENDING order.

MATCH (people:Person)
WHERE people.age > 27 RETURN people
ORDER BY people.age DESC
Filtering and ordering nodes
Filtered and ordered nodes
Filtered and ordered nodes

Aggregation

We can also use mathematical operations in our queries. For that, first, we collect our nodes with our MATCH command, then we use operators like COUNT or AVG with the RETURN command to calculate our desired values.

MATCH (people:Person) RETURN COUNT(people)
MATCH (people:Person) RETURN AVG(people.age)
Calculating nodes
Calculated values
Calculated values

Constraints

We can also use another type of filter query called a constraint. We can use this while creating our dataset. For this, we use the CREATE command followed by the CONSTRAINT ON keyword; then we create our (variable:Label). Now we add our constraint, i.e., ASSERT based on some property field followed by what the assertion is ie IS UNIQUE.

CREATE CONSTRAINT ON (person:Person) ASSERT person.email IS UNIQUE
Creating a constraint
Created constraint
Created constraint

Indexing

To create faster queries, we can use the indexing method. This allows us to search based on the index data structure. First, we use the CREATE command followed by the INDEX command to create our index. Then we use the ON :Label(property) command to assign it to the label and the property.

CREATE INDEX ON :Movie(title)
Created index on label's property

Conclusion

Neo4j and the Cypher query language allow us to use these fundamental queries and operations to make sense of our dataset. Moreover, optimization strategies inbuilt and those we can implement help create a faster system where it is needed without hindering the other processes. Moreover, we can use these operations together to perform complex querying and locating our required data.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved