Representative of the graph database: neo4j official website: http://neo4j.com/
Introduction: Why use a graph database
In many new projects, the application of graph database is an imperative trend, because the graph database can be a good representation of the concept of various nodes and relationships, and can be very friendly visualization, greatly facilitates our data management and display work.
The following is an introduction to the graphic relationships of actors, directors and films:
A concept
Node:
(a)//actors (m)//movies ()//some anonymous nod
Relationship:
// as " R " (a) // as " R " to movies // is acted_in (a)-[:acted_in]->//actors that acted_in some movie (d)// Directors that DIRECTED some movie
Property:
(M {title:"the Matrix"//Movie with a title property (a {name:" Keanu Reeves ", Born:1964//Actor with Name and born property
(a)-[:ACTED_IN {roles:["Neo"]}]->(m)
Relationship acted_in with roles Property (an array of character names)
Label:
//a person (A:person {name:"Keanu Reeves"//a person with Properties (A:person)//a person that acted_in some movie
Two CQL query statement-match
Running environment for online browser-database is the official actor-movie graph database
1 Querying all nodes
MATCH (n) RETURN N;
Query two nodes, one edge:
MATCH (N)---(m) RETURN N, m;
A node can also be missing
MATCH (n)--and () RETURN N;
Returns the attribute value of a node: Return the name of the actor and director
MATCH (person)--() RETURN person.name;
2 Query relationships-edge
To query all of the edge relationships:
MATCH (person)-[rel]->(in movie) RETURN person, type (rel)
Find out which actor played which movie:
MATCH (actor)-[role:acted_in]->(movie) RETURN actor.name, Role.roles, Movie.title;
3 Query Tags:
Use tags to find out which actor has performed which movie:
MATCH (Actor:person)-[:acted_in]->(movie) RETURN actor.name, Movie.title;
Add the WHERE condition filter, the actor named Tom Hanks :
" Tom Hanks " RETURN Tom;
You can also do this:
MATCH (Tom:person {name:"Tom Hanks"}) RETURN Tom;
4 Path: transitive relationship
Find out which actor is in the movie, and the director diagram of the film:
MATCH (actor)-[:acted_in]-> (movie) <-[:D irected]-(director) RETURN Actor.name, Movie.title, Director.name;
Another notation for the above statement:
MATCH (actor)-[:acted_in]->(movie), <-[:D irected]-(director) RETURN Actor.name, Movie.title, director.name;
One more equivalent notation:
MATCH (actor)-[:acted_in]-> ( Director)-[:D irected]->(movie) RETURN Actor.name, Movie.title, director.name;
Define Path
Return multiple paths and their properties with P:
MATCH p= (a)-[:acted_in]-> (m) <-[:D irected]-(d) RETURN p;
Return path with node:
MATCH p= (a)-[:acted_in]-> (m) <-[:D irected]-(d) RETURN nodes (p);
Return path with relationship rels:
MATCH p= (a)-[:acted_in]-> (m) <-[:D irected]-(d) RETURN rels (p);
Path definition for two modes:
MATCH p1= (a)-[:acted_in]-> (m), p2= (d)-[:D irected]->(m) RETURN P1, p2;
Through the above introduction, is not you also found that the potential of the graph database is endless, as long as the need to use the project, neo4j very useful. We hope to follow up on the advanced introduction.
NEO4J: Graph database Graphdb (i)