neo4j cypher update existing node or create new node

后端 未结 2 1264
走了就别回头了
走了就别回头了 2021-02-03 11:27

I have a graph with approximately nine million nodes, and twelve million relationships. For each of the nodes within the graph there is a subset of properties for each respectiv

相关标签:
2条回答
  • 2021-02-03 12:19

    Just a naive approach: what if you run a MERGE and just create or update it?

    Given your list of records, consider each record as a map:

    { first: "fred", last: "lake", height: 201 }
    { first: "barry", last: "smith", language: "english" }
    { first: "fred", last: "jones", language: "welsh", height: 188 }
    { first: "fred", last: "jones", eyes: "brown" }
    { first: "barry", last: "smith" }
    

    Then write your query in a parametric way:

    MERGE (p:Person{ first: { map }.name, last: { map }.last }
    ON CREATE SET n = { map }
    ON MATCH  SET n += { map }
    

    Description of the query:

    • In case of creation it should create a new node using all the properties passed in the {map}
    • In case of matching it should add new properties to the node without deleting any

    I've run some queries in console of the page linked above with a MERGE ON MATCH and it seems to update existing properties to new values. The queries I've run are the following:

    MATCH (peter { name: 'Peter' }) RETURN peter
    MERGE (peter { name: 'Peter' }) ON MATCH SET peter += { hungry: TRUE , position: 'Entrepreneur' }
    MATCH (peter { name: 'Peter' }) RETURN peter
    // added two new properties here
    MERGE (peter { name: 'Peter' }) ON MATCH SET peter += { hungry: FALSE , position: 'Entrepreneur' }
    MATCH (peter { name: 'Peter' }) RETURN peter
    // hungry is now false in here
    
    0 讨论(0)
  • 2021-02-03 12:24

    I'd say that this is the best way. Depending on the Neo4j interface you are using, you could write a single query that would handle everything without custom SET commands, but I'm guessing that you were just simplifying the question and have that covered.

    0 讨论(0)
提交回复
热议问题