Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Thursday, 29 February 2024

What is Match in neo4j

In Neo4j's Cypher Query Language, the MATCH clause is used to specify patterns of nodes and relationships in the graph that you want to retrieve or work with. It is one of the fundamental clauses in Cypher and is commonly used for querying data.


Syntax:


MATCH (variable:Label)

WHERE condition

RETURN variable


Example:

Suppose we have a simple graph of Person nodes, each with a name property. We want to retrieve all Person nodes from the graph.


MATCH (p:Person)

RETURN p


In this example:

- (p:Person) defines a pattern where p is a variable representing nodes labeled as Person.

- MATCH (p:Person) retrieves all nodes that match this pattern, assigning them to the variable p.

- RETURN p specifies that we want to return the matched nodes.


Pattern Matching:

The MATCH clause allows you to specify more complex patterns involving nodes and relationships. For example, you can specify patterns with multiple nodes and relationships to traverse the graph and retrieve connected data.


MATCH (p1:Person)-[:FRIENDS_WITH]->(p2:Person)

RETURN p1, p2


In this example:

- (p1:Person)-[:FRIENDS_WITH]->(p2:Person) defines a pattern where p1 and p2 are variables representing nodes labeled as Person, connected by a FRIENDS_WITH relationship.

- MATCH (p1:Person)-[:FRIENDS_WITH]->(p2:Person) retrieves all pairs of Person nodes that are connected by a FRIENDS_WITH relationship.


Using Conditions with MATCH:

You can also use the WHERE clause to apply conditions to filter the matched nodes based on certain criteria.


MATCH (p:Person)

WHERE p.age > 30

RETURN p


In this example:

- WHERE p.age > 30 filters the matched Person nodes based on the condition that their age property is greater than 30.


The MATCH clause is a fundamental part of Cypher queries in Neo4j, allowing you to specify patterns of nodes and relationships to retrieve or work with data in the graph.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.