merge可以看成是match和create的合体
1.找不到标签则创建
MERGE (robert:Critic)
RETURN robert, labels(robert)
2.找不到属性则创建
MERGE (charlie { name: 'Charlie Sheen', age: 10 })
RETURN charlie
3.找不到标签和属性则创建
MERGE (michael:Person { name: 'Michael Douglas' })
RETURN michael.name, michael.bornIn
5.根据已有的节点属性创建
MATCH (person:Person)
MERGE (city:City { name: person.bornIn })
RETURN person.name, person.bornIn, city
6.在创建的时候使用on create(在创建时进行一些操作)
MERGE (keanu:Person { name: 'Keanu Reeves' })
ON CREATE SET keanu.created = timestamp()
RETURN keanu.name, keanu.created
7.在创建的时候使用 on match
MERGE (person:Person)
ON MATCH SET person.found = TRUE RETURN person.name, person.found
8.同时使用on create 和 on match
MERGE (keanu:Person { name: 'Keanu Reeves' })
ON CREATE SET keanu.created = timestamp()
ON MATCH SET keanu.lastSeen = timestamp()
RETURN keanu.name, keanu.created, keanu.lastSeen
9.on match 设置多个属性
MERGE (person:Person)
ON MATCH SET person.found = TRUE , person.lastAccessed = timestamp()
RETURN person.name, person.found, person.lastAccessed
MATCH (charlie:Person { name: 'Charlie Sheen' }),(wallStreet:Movie { title: 'Wall Street' })
MERGE (charlie)-[r:ACTED_IN]->(wallStreet)
RETURN charlie.name, type(r), wallStreet.title
参考文献:
https://neo4j.com/docs/developer-manual/3.4/cypher/clauses/merge/