Memgraph logo
Back to blog
Cypher Differences Between Neo4j and Memgraph

Cypher Differences Between Neo4j and Memgraph

By Katarina Supe
7 min readAugust 21, 2024

Curious what are the Cypher differences between Neo4j and Memgraph? This blog post will guide you through the essential differences in Cypher syntax and usage between Neo4j and Memgraph.

Memgraph uses Cypher, implemented according to the openCypher standard. While Memgraph follows this standard, there are some differences in how it implements Cypher compared to other graph databases that also adhere to the openCypher standard. These differences are designed to enhance the user experience and make querying more efficient.

Knowing these differences will make your transition easier if you're migrating from Neo4j to Memgraph.

Understanding Indexes and Constraints

Indexes in graph databases optimize performance by enabling fast data retrieval and efficient query execution, which is crucial for handling complex queries on large datasets. Constraints ensure data integrity and consistency by enforcing rules and preventing invalid data entries.

In Neo4j, indexes and constraints are crucial to optimizing queries and ensuring data integrity. For example, to create a range index in Neo4j, you might use the following Cypher query:

CREATE INDEX node_range_index_name FOR (n:Person) ON (n.surname);

Memgraph, on the other hand, offers several types of indexes, such as label, label-property, edge-type, and edge-type property indexes. While the syntax differs slightly, it remains intuitive.

For example, this is how you would create a label-property index in Memgraph:

CREATE INDEX ON :Person(surname);

This difference is minor but important to note when migrating your indexing strategies.

Similarly, when working with constraints, Neo4j uses the following syntax to ensure that a specific property is always present:

CREATE CONSTRAINT author_name
FOR (author:Author) 
REQUIRE author.name IS NOT NULL;

In Memgraph, this translates to:

CREATE CONSTRAINT ON (author:Author) 
ASSERT EXISTS (author.name);

A key difference to note is that in Memgraph, creating a constraint does not automatically create an index, so you may need to explicitly create indexes to ensure optimal performance.

To ensure fast query response and performant data import, follow our querying and import best practices.

Cypher Syntax Variations

While both Neo4j and Memgraph use Cypher, there are some important syntax differences that you need to be aware of, especially for specific query types like path traversals.

Deep Path Traversals

Path Traversals are fundamental in graph databases, used to explore relationships between nodes.

Memgraph provides built-in algorithms for deep path traversals, such as BFS (Breadth-First Search), DFS (Depth-First Search), WSP (Weighted Shortest Path), and ASP (All Shortest Paths).

In Neo4j, finding the shortest path between two nodes might look like this:

MATCH p=shortestPath(
(:Person {name:"Keanu Reeves"})-[*]-(:Person {name:"Tom Hanks"})
)
RETURN p

Memgraph simplifies this with built-in algorithms, making path traversal more efficient:

MATCH p=(:Person {name:"Keanu Reeves"})-[*BFS]-(:Person {name:"Tom Hanks"})
RETURN p

These built-in algorithms, such as BFS (Breadth-First Search), DFS (Depth-First Search), and others, provide powerful tools for deep path traversal and can significantly enhance query performance.

Read more:

Functions and Procedures: Migrating APOC and GDS to MAGE

Memgraph supports most of the common Cypher functions available in Neo4j, but there are some differences in function names and behaviors.

If you've been using Neo4j's APOC (Awesome Procedures on Cypher) or GDS (Graph Data Science library), you might be concerned about losing these functionalities when moving to Memgraph. Fortunately, Memgraph offers MAGE(Memgraph Advanced Graph Extensions), which includes a wide range of prebuilt static and dynamic graph algorithms and utility functions. These ease the migration process and improve Cypher query flexibility.

If you find a particular function missing, let us know on GitHub or create custom query modules to extend its functionality further.

Read more:

Data Manipulation and Query Performance

Migrating basic data manipulation operations like CREATE, MATCH, MERGE, and DELETE from Neo4j to Memgraph generally requires only minor adjustments.

However, developers should be mindful of performance considerations. Memgraph is designed for high performance and real-time analytics, which may require some adjustments to your query optimization strategies compared to Neo4j.

Memgraph is an in-memory database, which typically offers faster query execution than Neo4j. However, to fully use Memgraph's performance, you might need to tweak your queries or optimize your indexing strategies. If you're experiencing slower performance than expected, Memgraph provides tools and support to help you fine-tune your queries.

Read more:

If you are unsure how to tweak your queries, reach out to our Developer Experience team for help.

Using Memgraph’s Unique Features

Memgraph extends Cypher language with unique features designed to enhance real-time data processing and analytics. These features include:

  • Deep path traversals
  • Dynamic graph algorithms
  • Custom query modules

Conclusion

If you are looking to migrate from Neo4j to Memgraph, understanding differences in Cypher implementation is a good way to start.

Read more:

Next Steps?

To truly understand how Cypher works in Memgraph and experience the differences firsthand, we recommend trying out the Memgraph Playground sandbox. It's an interactive environment where you can experiment with Cypher queries and see how they perform in Memgraph.

Go to Memgraph Playground.

Join us on Discord!
Find other developers performing graph analytics in real time with Memgraph.
© 2024 Memgraph Ltd. All rights reserved.