Difference between revisions of "Neo4j"

From John Freier
Jump to: navigation, search
(Neo4J Export)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
 +
== Neo4J ==
 +
 +
Start Up
 +
  bin/neo4j start
 +
 +
as a script
 +
  bin/neo4j $1
 +
 +
For development use these items may want to be turned off,
 +
  SSL - ./conf/neo4j-server.properties ~ org.neo4j.server.webserver.https.enabled - PORT: 7473
 +
  Online Backup -  ./conf/neo4j.properties ~ online_backup_enabled - PORT: 6362
  
 
== Sample Cypher ==
 
== Sample Cypher ==
Line 4: Line 16:
 
Create Node
 
Create Node
 
   CREATE (n:Label {property:'value'}) return n;
 
   CREATE (n:Label {property:'value'}) return n;
 +
 +
Create Node with multiple labels
 +
  CREATE (n:Label1:Label2 {property:'value'}) return n;
 +
 +
Create a Relationship
 +
  MATCH (n1:Node), (n2:Node) CREATE (n1)-[r:RELATIONSHIP]->(n2); <-- Not sure this is correct.
 +
 
 +
  MATCH (n1:Node), (n2:Node) MERGE (n1)-[r:RELATIONSHIP]->(n2); <-- This I believe should be used.
 +
 +
  CREATE (n1:Node)-[r:RELATIONSHIP]-(n2:Node) return n1, n2;
  
 
Find Node By Id
 
Find Node By Id
 
   MATCH (n:Label) WHERE Id(n) = 0 RETURN n;
 
   MATCH (n:Label) WHERE Id(n) = 0 RETURN n;
 +
 +
Update Nodes properties
 +
  MATCH (n { name: 'John' }) SET n.surname = 'Doe' RETURN n
  
 
Delete Node by Id
 
Delete Node by Id
 
   MATCH (n:Node) WHERE Id(n) = 0 DELETE n;
 
   MATCH (n:Node) WHERE Id(n) = 0 DELETE n;
 +
 +
Delete Relationship
 +
  MATCH (n:Node)-[r:Relationship]-(n:Node) DELETE r;
 +
 +
Delete Relationship by id
 +
  MATCH ()-[r]-() WHERE id(r)={id} DELETE r
  
 
Delete All Nodes and Relationships
 
Delete All Nodes and Relationships
Line 18: Line 49:
 
   MATCH (n) DETACH DELETE n
 
   MATCH (n) DETACH DELETE n
  
Update Nodes properties
+
Delete a node and all relationships attached.
   MATCH (n { name: 'John' }) SET n.surname = 'Doe' RETURN n
+
   MATCH (n) WHERE id(n) = 1  DETACH DELETE n
  
Create a Relationship
+
Delete a property from a node
   MATCH (n1:Node), (n2:Node) CREATE (n1)-[r:RELATIONSHIP]->(n2);
+
   MATCH (n:Node) WHERE id(n) = 0 REMOVE n.oldProperty RETURN n;
  
 +
Add/Remove label from node
 +
  MATCH (n) WHERE ID(n) = 1 REMOVE n:Lable1 SET n:Label2
  
 
== Neo4J Export ==
 
== Neo4J Export ==

Latest revision as of 00:03, 11 November 2017

Neo4J

Start Up

 bin/neo4j start

as a script

 bin/neo4j $1

For development use these items may want to be turned off,

 SSL - ./conf/neo4j-server.properties ~ org.neo4j.server.webserver.https.enabled - PORT: 7473
 Online Backup -  ./conf/neo4j.properties ~ online_backup_enabled - PORT: 6362

Sample Cypher

Create Node

 CREATE (n:Label {property:'value'}) return n;

Create Node with multiple labels

 CREATE (n:Label1:Label2 {property:'value'}) return n;

Create a Relationship

 MATCH (n1:Node), (n2:Node) CREATE (n1)-[r:RELATIONSHIP]->(n2); <-- Not sure this is correct.
 
 MATCH (n1:Node), (n2:Node) MERGE (n1)-[r:RELATIONSHIP]->(n2); <-- This I believe should be used.
 CREATE (n1:Node)-[r:RELATIONSHIP]-(n2:Node) return n1, n2;

Find Node By Id

 MATCH (n:Label) WHERE Id(n) = 0 RETURN n;

Update Nodes properties

 MATCH (n { name: 'John' }) SET n.surname = 'Doe' RETURN n

Delete Node by Id

 MATCH (n:Node) WHERE Id(n) = 0 DELETE n;

Delete Relationship

 MATCH (n:Node)-[r:Relationship]-(n:Node) DELETE r;

Delete Relationship by id

 MATCH ()-[r]-() WHERE id(r)={id} DELETE r

Delete All Nodes and Relationships pre 2.3.0

 MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r;

2.3.0 +

 MATCH (n) DETACH DELETE n

Delete a node and all relationships attached.

 MATCH (n) WHERE id(n) = 1  DETACH DELETE n

Delete a property from a node

 MATCH (n:Node) WHERE id(n) = 0 REMOVE n.oldProperty RETURN n;

Add/Remove label from node

 MATCH (n) WHERE ID(n) = 1 REMOVE n:Lable1 SET n:Label2

Neo4J Export

Export database as Cypher

 bin/neo4j-shell -c dump > ./filename.cypher

Import database as Cypher

 bin/neo4j-shell -file ./filename.cypher

Execute database statement

 bin/neo4j-shell -c "MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r;"