Neo4j: Graph Database

Create a node

CREATE ();
CREATE (:Label1);
CREATE (:Label1:Label2:LabelN);
CREATE (:Label1 {name: "James Bond"})
CREATE (n) RETURN n;
CREATE (n:Label1) RETURN n;
CREATE (n:Label1:Label2:LabelN) RETURN n;
CREATE (:Label1 {name: "James Bond", ...}) RETURN n;

Query nodes

MATCH (n) RETURN n;
MATCH (n) RETURN n WHERE ID(n)=1;
MATCH (n) RETURN n LIMIT 25;
MATCH (n:Label1) RETURN n LIMIT 25;

Create a relationship

CREATE (:Person {name: "Andy"})-[:KNOWS]->(:Person {name: "John");

MATCH (a:Person) WHERE a.name="Andy"
MATCH (b:Person) WHERE b.name="John"
CREATE (a)-[:KNOWS]->(b);

Query by relationship

MATCH p=()-[:RELATIONSHIP]->() RETURN p;
MATCH p=()-[:RELATIONSHIP*]->() RETURN p;
MATCH p=()-[:RELATIONSHIP*1]->() RETURN p;
MATCH p=()-[:RELATIONSHIP*1..1]->() RETURN p;
MATCH p=()-[r:RELATIONSHIP]->()
WHERE r.condition=value
RETURN p;
MATCH p=()-[r:RELATIONSHIP]->(b)
WHERE r.condition=value
AND b.condition=value
RETURN p;

Query relationship by node

MATCH (n:NODE1)-(r)-(b) RETURN n, r, b;
MATCH (n:NODE1)-(r)-(b) RETURN *;
MATCH (n:NODE1)-(r*1)-(b) RETURN *;
MATCH (n:NODE1)-(r*1..1)-(b) RETURN *;

References


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *