Neo4j Cheat Sheet – How to do certain things in Neo4j Graph Database

Add Stuffs:

  1. Create a simple node
    CREATE (n:NodeLabel) return n;
  2. Create a node with some node properties
    CREATE (n:NodeLabel{propertyName1:”propertyValue1″, propertyName2:”propertyValue2″}) return n
  3. Create a simple relationship between two nodes.
    Match (m:SomeNode),(n:SomeOtherNode) 
    CREATE (m)-[r:SOME_RELATION]->(n)
    return m,n
  4. Create a relationship between two nodes if the relationship already does not exist
    Match (m:SomeNode),(n:SomeOtherNode) 
    CREATE UNIQUE (m)-[r:SOME_RELATION]->(n)
    return m,n
  5. Create a relationship between two nodes with certain properties
    Match (m:SomeNode{someProperty:”SomePropertyValue”}),
    (n:SomeOtherNode{someProperty:”SomePropertyValue”}) 
    CREATE (m)-[r:SOME_RELATION]->(n)
    return m,n
  6. Add a property to a relationship
    Match g=()-[r:Some_Relationship]-()
    set r.some_property_name=”Some_Property_Name_Of_Relationship” return g;

Delete Stuffs:

  1. Delete all relationships for a node by node property
    MATCH (n:SomeLabel{ name: ”SomeNodeProperty” })-[r]->() DELETE r
  2. Delete all relationships by a relationship name
    MATCH (n:SomeNode)-[r:SomeRelationShip]->(a:OtherNode) DELETE r
  3. Delete a relationship by relationship ID
    start r=rel(someRelationshipID) delete r;
  4. Delete a node property
    MATCH(n:SomeNode) REMOVE n.someNodeProperty return n

Rename Stuffs:

  1. How to rename relationships
    MATCH (n1)-[r1:OldRelationShip]->(n2)
    CREATE (n1)-[r2:NewRelationship]->(n2)
    DELETE old
  2. How to rename Node labels
    MATCH (n) WHERE ID(n) = 14
    REMOVE n:Developers
    SET n:Developer

Plan Queries:

  1. How to see the query plan without committing
    EXPLAIN MATCH n RETURN n
  2. How to see the query plan with commit
    PROFILE MATCH n RETURN n

 

Tagged , , , , , . Bookmark the permalink.

One Response to Neo4j Cheat Sheet – How to do certain things in Neo4j Graph Database

  1. adam says:

    I tried almost every combination to load my data sample from csv to neo4j but it doesnt work. can you help me ? i am trying to load from my local machine and it is giving understandable errors.

Leave a Reply

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