美文网首页
Neo4j - Write Clause

Neo4j - Write Clause

作者: 韩维思 | 来源:发表于2018-11-21 19:16 被阅读0次

    1. Merge

    MERGE command is a combination of CREATE command and MATCH command.
    Neo4j CQL MERGE command searches for a given pattern in the graph. If it exists, then it returns the results.
    If it does NOT exist in the graph, then it creates a new node/relationship and returns the results.
    MERGE (node: label {properties . . . . . . . })
    Following is the syntax for the MERGE command.
    MERGE (node:label) RETURN node
    eg1.
    Before proceeding to the examples in this section, create two nodes in the database with labels Dhawan and Ind. Create a relationship of type “BATSMAN_OF” from Dhawan to Ind as shown below.

    CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"}) 
    CREATE (Ind:Country {name: "India"}) 
    CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind) 
    #上一节已经创建过了
    

    1.1 Merging a Node with a Label

    MERGE (node:label) RETURN node
    eg.
    MERGE (Jadeja:player) RETURN Jadeja
    On executing, you will get the following result. Since you have already created a node named “Dhawan” with the label “player” in the database, Neo4j returns it as shown in the following screenshot.

    image.png

    eg2.
    Now, try to merge a node named “CT2013” with a label named Tournament. Since there are no nodes with this label, Neo4j creates a node with the given name and returns it.

    MERGE (CT2013:Tournament{name: "ICC Champions Trophy 2013"}) 
    RETURN CT2013, labels(CT2013)
    

    1.2 Merging a Node with Properties

    Neo4j searches for an equal match for the specified node, including the properties. If it doesn’t find any, it creates one.
    MERGE (node:label {key1:value, key2:value, key3:value . . . . . . . . })

    eg.
    Following is a sample Cypher Query to merge a node using properties. This query tries to merge the node named “jadeja” using properties and label. Since there is no such node with the exact label and properties, Neo4j creates one.

    MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"}) 
    RETURN Jadeja 
    

    Since there are no nodes with the specified label and properties, it creates one, as shown in the following screenshot.


    image.png

    1.3 OnCreate and OnMatch

    Whenever, we execute a merge query, a node is either matched or created. Using on create and on match, you can set properties for indicating whether the node is created or matched.

    MERGE (node:label {properties . . . . . . . . . . .}) 
    ON CREATE SET property.isCreated ="true" 
    ON MATCH SET property.isFound ="true"
    

    If the specified node already exists in the database, then the node will be matched and the property with key-value pair isFound = "true" will be created in the node. If the specified node doesn’t exist in the database, then the node will be created, and within it a property with a key-value pair isCreated ="true" will be created.

    MERGE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"}) 
    ON CREATE SET Jadeja.isCreated = "true" 
    ON MATCH SET Jadeja.isFound = "true" 
    RETURN Jadeja 
    
    image.png

    1.4 Merge a Relationship

    Just like nodes, you can also merge the relationships using the MERGE clause.
    eg.
    Following is a sample Cypher Query which merges a relationship using the MATCH clause in Neo4j. This query tries to merge a relationship named WINNERS_OF between the nodes “ind” (label: Country & name: India) and ICC13 (label: Tournament & name: ICC Champions Trophy 2013).

    MATCH (a:Country), (b:Tournament) 
       WHERE a.name = "India" AND b.name = "ICC Champions Trophy 2013" 
       MERGE (a)-[r:WINNERS_OF]->(b) 
    RETURN a, b 
    

    Since such relation doesn’t exist, Neo4j creates one.


    image.png

    2. Set Clause

    2.1 Setting a Property

    Using the SET clause, you can create a new property in a node.

    MATCH (node:label{properties . . . . . . . . . . . . . . }) 
    SET node.property = value 
    RETURN node
    

    eg.
    Before proceeding with the example, first create a node named Dhawan as shown below:
    CREATE (Dhawan:player{name: "shikar Dhawan", YOB: 1985, POB: "Delhi"})
    Following is a sample Cypher Query to create a property named “highestscore” with value “187”.

    MATCH (Dhawan:player{name: "shikar Dhawan", YOB: 1985, POB: "Delhi"}) 
    SET Dhawan.highestscore = 187 
    RETURN Dhawan
    

    Here you can observe that a property with a key-value pair highestscore/187 is created in the node named “Dhawan”.


    Set Property of Existing Node

    2.2 Removing a Property

    You can remove an existing property by passing NULL as value to it.

    MATCH (node:label {properties}) 
    SET node.property = NULL 
    RETURN node 
    

    eg.
    Before proceeding with the example, first create a node “jadeja” as shown below.
    Ceate (Jadeja:player{name:"Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"})
    Following is a sample Cypher Query which removes the property named POB from this node using the SET clause as shown below.

    MATCH (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB: "NavagamGhed"}) 
    SET Jadeja.POB = NULL 
    RETURN Jadeja 
    
    Remove Property ‘POB’

    2.3 Setting Multiple Properties

    To set multile properties in one node, you need to specify these key value pairs with commas.

    MATCH (node:label {properties}) 
    SET node.property1 = value, node.property2 = value 
    RETURN node 
    

    eg.

    MATCH (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988})  
    SET Jadeja.POB: "NavagamGhed", Jadeja.HS = "90" 
    RETURN Jadeja
    

    Here you can observe that properties named POB and HS were created.


    image.png

    2.4 Setting a Label on a Node

    MATCH (n {properties . . . . . . . }) 
    SET n :label 
    RETURN n 
    

    eg.
    Before proceeding with the example, first create a node “Anderson” as shown below.
    CREATE (Anderson {name: "James Anderson", YOB: 1982, POB: "Burnely"})
    This query adds the label “player” to the node Anderson and returns it.

    MATCH (Anderson {name: "James Anderson", YOB: 1982, POB: "Burnely"}) 
    SET Anderson: player 
    RETURN Anderson 
    
    Single Lable

    2.5 Setting Multiple Labels on a Node

    You can set multiple labels to an existing node using the SET clause. Here you need to specify the labels by separating them with colons “:”.

    MATCH (n {properties . . . . . . . }) 
    SET n :label1:label2 
    RETURN n 
    

    eg.
    Before proceeding with the example, first create a node named “Ishant” as shown below.
    CREATE (Ishant {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})

    MATCH (Ishant {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"}) 
    SET Ishant: player:person 
    RETURN Ishant 
    

    Here you can observe that two labels - person and player – are added to the node named Ishant.


    Multiple Labels

    3. Delete Clause

    You can delete nodes and relationships from a database using the DELETE clause.

    3.1 Deleting All Nodes and Relationships

    Query: MATCH (n) DETACH DELETE n

    3.2 Deleting a Particular Node

    To delete a particular node, you need to specify the details of the node in the place of “n” in the above query.

    MATCH (node:label {properties . . . . . . . . . .  }) 
    DETACH DELETE node
    

    eg.
    Before proceeding with the example, create a node “Ishant” in the Neo4j database as shown below.
    CREATE (Ishant:player {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
    Deletes the above created node using the DELETE clause.

    MATCH (Ishant:player {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"}) 
    DETACH DELETE Ishant
    
    Delete

    4. Remove Clause

    \color{red}{移除一个属性用xx.xx,移除一个标签用xx:xx}
    The REMOVE clause is used to remove properties and labels from graph elements (Nodes or Relationships).

    The main difference between Neo4j CQL DELETE and REMOVE commands is −

    DELETE operation is used to delete nodes and associated relationships.
    REMOVE operation is used to remove labels and properties.

    4.1 Removing a Property

    You can remove a property of a node using MATCH along with the REMOVE clause.

    MATCH (node:label{properties . . . . . . . }) 
    REMOVE node.property 
    RETURN node 
    

    eg.
    Before proceeding with the example, create a node named Dhoni as shown below.
    CREATE (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})

    MATCH (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) 
    REMOVE Dhoni.POB 
    RETURN Dhoni 
    
    Remove a Property

    4.2 Removing a Label From a Node

    MATCH (node:label {properties . . . . . . . . . . . }) 
    REMOVE node:label 
    RETURN node 
    

    eg.
    Remove a label from an existing node using the remove clause.

    MATCH (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) 
    REMOVE Dhoni:player 
    RETURN Dhoni 
    
    Remove a Label

    4.3 Removing Multiple Labels

    MATCH (node:label1:label2 {properties . . . . . . . . }) 
    REMOVE node:label1:label2 
    RETURN node
    

    eg.

    MATCH (Ishant:player:person {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"}) 
    REMOVE Ishant:player:person 
    RETURN Ishant 
    
    Remove Multiple Labels

    5. Foreach Clause

    The FOREACH clause is used to update data within a list whether components of a path, or result of aggregation.

    MATCH p = (start node)-[*]->(end node) 
    WHERE start.node = "node_name" AND end.node = "node_name" 
    FOREACH (n IN nodes(p)| SET n.marked = TRUE) 
    

    eg.
    Before proceeding with the example, create a path p in Neo4j database as shown below.

    CREATE p = (Dhawan {name:"Shikar Dhawan"})-[:TOPSCORRER_OF]->(Ind{name: 
       "India"})-[:WINNER_OF]->(CT2013{name: "Champions Trophy 2013"}) 
    RETURN p 
    

    Following is a sample Cypher Query which adds a property to all the nodes along the path using the FOREACH clause.

    MATCH p = (Dhawan)-[*]->(CT2013) 
       WHERE Dhawan.name = "Shikar Dhawan" AND CT2013.name = "Champions Trophy 2013" 
    FOREACH (n IN nodes(p)| SET n.marked = TRUE)
    
    Foreach Result
    Foreach Verification

    相关文章

      网友评论

          本文标题:Neo4j - Write Clause

          本文链接:https://www.haomeiwen.com/subject/nlxhqqtx.html