📅  最后修改于: 2023-12-03 14:40:24.634000             🧑  作者: Mango
Cypher is a query language developed by Neo4j for querying and manipulating graph data stored in their database management system, Neo4j.
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.
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.
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.
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'.
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.
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.
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.