使用Cypher更新属性的正确方法是:

huangapple go评论56阅读模式
英文:

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

使用Cypher更新属性的正确方法是:

PROFILE MATCH (p:Person) WHERE p.name= "John" SET p.name="Johan";

使用Cypher更新属性的正确方法是:

答案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

huangapple
  • 本文由 发表于 2023年3月8日 18:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定