英文:
What is a correct way to update a property using Cypher?
问题
以下是翻译好的部分:
Query 1:
MATCH (p:Person)
WHERE p.name= "John"
SET p += {name: "Johan"};
Query 2:
MATCH (p:Person)
WHERE p.name= "John"
SET p.name="Johan";
I've run the PROFILE
for both queries, and they are rather similar.
英文:
I have nodes that have first name. I want to update all of the names John to Johan. Is there a difference between this two queries? The end result seems the same to me.
Query 1
MATCH (p:Person)
WHERE p.name= "John"
SET p += {name: "Johan"};
Query 2
MATCH (p:Person)
WHERE p.name= "John"
SET p.name="Johan";
I've run the PROFILE
for both queries, and they are rather similar.
PROFILE MATCH (p:Person) WHERE p.name= "John" SET p += {name: "Johan"};
PROFILE MATCH (p:Person) WHERE p.name= "John" SET p.name="Johan";
答案1
得分: 1
有一个“差异”,因为查询 1 使用 SetProperties
操作从地图中复制 所有 属性(该地图碰巧只有一个属性),而查询 2 使用 SetProperty
从文字中设置一个属性。
我认为使用 +=
语法的最重要原因是当需要设置很多属性时。在这种情况下,编写:
SET p += x
比编写以下方式更容易(且不容易出错):
SET p.a = x.a, p.b = x.b, p.c = x.c, ...
或者
SET p.a = 123, p.b = true, p.c = 'Fred', ...
特别是如果查询中的先前处理免费提供了 x
。但即使必须创建 x
地图,结果更易阅读:
WITH p, {a: 123, b: true, c: 'Fred', ...} AS x
SET p += x
英文:
There certainly is a "difference", since Query 1 uses the SetProperties
operation to copy all the properties from a map (that happens to have a single property), whereas Query 2 uses SetProperty
to set a single property from a literal.
I think the most important reason for using the +=
syntax occurs when there are a lot of properties to be set. In that case, it is a lot easier (and less brittle) to write:
SET p += x
than:
SET p.a = x.a, p.b = x.b, p.c = x.c, ...
or
SET p.a = 123, p.b = true, p.c = 'Fred', ...
Especially if processing earlier in the query provides x
for free. But even if you have to create the x
map, the result is more readable:
WITH p, {a: 123, b: true, c: 'Fred', ...} AS x
SET p += x
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论