📜  cypher neo4j (1)

📅  最后修改于: 2023-12-03 14:40:24.634000             🧑  作者: Mango

Cypher Neo4j

Cypher is a query language developed by Neo4j for querying and manipulating graph data stored in their database management system, Neo4j.

Quick Facts
  • Cypher was released in 2012
  • Cypher is designed to be similar to SQL so that developers who are familiar with SQL can quickly get up to speed with Cypher
  • Cypher is a declarative query language that expresses the logic of the query without specifying how to do it
  • Cypher has a concise and readable syntax
  • Cypher supports pattern matching, filtering, aggregation, and more
  • Cypher is used by Neo4j for querying and manipulating graph data, but it can also be used with other databases that support the OpenCypher project
Get Started

To get started with Cypher, you'll need to have access to a Neo4j database. If you don't have one set up, you can get started with the Neo4j Sandbox, which provides a free and easy-to-use cloud-based environment for testing and learning.

Creating Nodes and Relationships

To create nodes and relationships in Cypher, you can use the CREATE keyword followed by the node or relationship to create. Here's an example:

CREATE (:Person {name: 'Alice'})-[:KNOWS]->(:Person {name: 'Bob'})

This creates two nodes labelled Person with properties name: 'Alice' and name: 'Bob', and creates a relationship of type KNOWS between them. Note that nodes and relationships can also have labels and properties.

Querying Nodes and Relationships

Once you have some data in your Neo4j database, you can use Cypher to query and retrieve that data. Here's an example query that retrieves all the Person nodes in the database:

MATCH (p:Person)
RETURN p

This uses the MATCH keyword to find all the nodes labelled Person, and the RETURN keyword to return those nodes.

Filtering Results

You can also filter your results in Cypher using the WHERE keyword. Here's an example query that retrieves all the Person nodes in the database whose name is 'Alice':

MATCH (p:Person)
WHERE p.name = 'Alice'
RETURN p

This uses the WHERE keyword to filter the results to only include nodes whose property name equals 'Alice'.

Querying Relationships

You can also use Cypher to query relationships between nodes. Here's an example query that retrieves all the Person nodes that are connected to each other by a KNOWS relationship:

MATCH (p1:Person)-[:KNOWS]->(p2:Person)
RETURN p1, p2

This uses the MATCH keyword to find all the nodes labelled Person that are connected by a KNOWS relationship, and the RETURN keyword to return both nodes.

Aggregating Results

Finally, you can use Cypher to aggregate your results using functions like COUNT, MIN, MAX, and AVG. Here's an example query that retrieves the number of Person nodes in the database:

MATCH (p:Person)
RETURN COUNT(p)

This uses the MATCH keyword to find all the nodes labelled Person, and the RETURN keyword with the COUNT function to return the number of nodes.

Conclusion

Cypher is a powerful and flexible query language that allows you to easily work with graph data stored in Neo4j. With its concise and readable syntax, and support for pattern matching, filtering, aggregation, and more, Cypher is a great choice for developers looking to work with graph data in their applications.