Update multiple nodes in a single query, each with different property / value pairs

不羁的心 提交于 2019-12-25 07:19:57

问题


A similar question was answered by Michael Hunger in early 2013 however I am unable to translate his response to Neo4j/Cypher 3.x.

https://groups.google.com/forum/#!msg/neo4j/qZWhbMtMCTE/r3W7OZfCgAgJ

Each node has at a property with a UUID value. In some cases the "second" property is a Boolean, in other cases a string.

I want to update some of these nodes, changing or adding a property to each one.

(n1 {uuid:"foo1", enabled: true})
(n2 {uuid:"foo2", example: "foo"})
(n3 {uuid:"foo3"})

I could of course create a separate MERGE and SET statement for each but I was hoping there was a more elegant solution:

MATCH (S {uuid:"foo0"})
MERGE (n2 {uuid:"foo2"})-[:BELONGS_TO]->(S)
    SET n2.example="bar"
MERGE (n3 {uuid:"foo3"})-[:BELONGS_TO]->(S)
    SET n3.enabled=true

回答1:


The following simple Cypher query can be used to add/update an arbitrary set of properties for any number of uuids. It will also create the node for a uuid if one does not already exist.

UNWIND {data} AS d
MERGE (x {uuid: d.uuid})
SET x += d.props;

It expects the {data} parameter to be an array of objects. The following array contains the same sample data as in your question:

[
  {uuid: 'foo1', props:{enabled: true}},
  {uuid: 'foo2', props:{example: 'foo'}},
  {uuid: 'foo3', props:{}}
]

In each data array element, the props object can contain any number of properties, and they will be added/updated accordingly in the node with the given uuid.



来源:https://stackoverflow.com/questions/38975496/update-multiple-nodes-in-a-single-query-each-with-different-property-value-pa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!