在单个查询中更新多个节点,每个节点具有不同的属性/值对 [英] Update multiple nodes in a single query, each with different property / value pairs

查看:210
本文介绍了在单个查询中更新多个节点,每个节点具有不同的属性/值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Michael Hunger在2013年初回答了类似的问题,但是我无法将他的回答翻译成Neo4j/Cypher 3.x.

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

每个节点都有一个具有UUID值的属性. 在某些情况下,"second"属性是布尔值,在其他情况下是字符串.

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"})

我当然可以为每个创建单独的MERGE和SET语句,但是我希望有一个更优雅的解决方案:

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

推荐答案

以下简单的Cypher查询可用于添加/更新任意数量的uuid的任意属性集.如果一个uuid还不存在,它还将为uuid创建一个节点.

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;

它期望{data} 参数为对象数组.以下数组包含与您的问题相同的示例数据:

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:{}}
]

在每个数据数组元素中,props对象可以包含任意数量的属性,并且将在具有给定uuid的节点中相应地添加/更新它们.

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.

这篇关于在单个查询中更新多个节点,每个节点具有不同的属性/值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆