在Neo4j中交换两个节点的特定属性,包括它们的关系。

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

Swapping specific properties of two nodes including relationships in Neo4j

问题

I'm trying to swap multiple properties and all relationships between two nodes in Neo4j.
For example-

Node 1 -> {id='id1', name:'ABC', city='London'} belongs to A
Node 2 -> {id='ID1', name:'PQR', city='Paris'} belongs to B

After Query Execution, the result should be-

Node 1 -> {id='id1', name:'PQR', city='Paris'} will belong to B
Node 2 -> {id='ID1', name:'ABC', city='London'} will belong to A

Note: id is case-sensitive hence two nodes with similar text are present. We don't want to swap id property.

Tried doing it with set properties(node) and Temp node like-

MATCH
(node1 {id: 'id1'}),
(node2 {id: 'ID1'})
CREATE (Temp:Student{properties(node1)})
AND SET node1.name = node2.name, node1.city = node2.city
AND SET node2.name = Temp.name, node2.city = Temp.city
AND DELETE Temp
RETURN node1,node2

Getting following error

Invalid input '(': expected "}" (line 4, column 33 (offset: 99))
"CREATE (Temp:Student{properties(node1)})"

Wanted to know if it's possible without creating a Temp node.

Thank you.

英文:

I'm trying to swap multiple properties and all relationships between two nodes in Neo4j.
For example-

Node 1 -> {id='id1',name:'ABC',city='London'} belongs to A
Node 2 -> {id='ID1',name:'PQR',city='Paris'} belongs to B

After Query Execution, the result should be-

Node 1 -> {id='id1',name:'PQR',city='Paris'} will belong to B
Node 2 -> {id='ID1',name:'ABC',city='London'} will belong to A

Note: id is case-sensitive hence two nodes with similar text are present. We don't want to swap id property.

Tried doing it with set properties(node) and Temp node like-

MATCH
  (node1 {id: 'id1'}),
  (node2 {id: 'ID1'})
CREATE (Temp:Student{properties(node1)}) 
AND SET node1.name = node2.name, node1.city = node2.city
AND SET node2.name = Temp.name, node2.city = Temp.city
AND DELETE Temp 
RETURN node1,node2

Getting following error

Invalid input '(': expected "}" (line 4, column 33 (offset: 99))
"CREATE (Temp:Student{properties(node1)})"

Wanted to know if it's possible without creating a Temp node.

Thank you.

答案1

得分: 1

以下查询对我有效:

MATCH (node1:Student {id: 'id1'}),(node2:Student {id: 'ID1'}) 
CREATE (temp:Student) 
SET temp = properties(node1),
    node1.name = node2.name, node1.city = node2.city,
    node2.name = temp.name, node2.city = temp.city
DELETE temp
RETURN node1, node2 

参考链接:https://neo4j.com/docs/cypher-manual/current/clauses/set/#set-copying-properties-between-nodes-and-relationships

如果不指定节点标签Student,对我来说是无法正常工作的。所以我需要将节点标签改为node1:Student,而不仅仅是node1。

英文:

Below query works for me:

MATCH (node1:Student {id: 'id1'}),(node2:Student {id: 'ID1'}) 
CREATE (temp:Student) 
SET temp  = properties(node1),
    node1.name = node2.name, node1.city = node2.city,
    node2.name = temp.name, node2.city = temp.city
DELETE temp
RETURN node1, node2 

Ref: https://neo4j.com/docs/cypher-manual/current/clauses/set/#set-copying-properties-between-nodes-and-relationships

Without specifying the node label Student, it is not working for me. So I need to put the label node1:Student instead of just node1.

答案2

得分: 1

这是我会编写查询的方式。你的结构是正确的,但有一些语法问题。最大的问题是,在创建Temp节点时,你需要使用SET子句。关于你是否可以避免创建临时节点的问题,我认为这是不可能的,因为通常会覆盖现有的属性。而且,如果你不覆盖它们,你将不得不保留/跟踪旧属性以进行移动。

MATCH
  (node1 {id: 'id1'}),
  (node2 {id: 'ID1'})
CREATE (Temp:Student {id: node1.id})
SET Temp = properties(node1) 
WITH node1, node2, Temp
SET node1 += {name: node2.name, city: node2.city},
    node2 += {name: Temp.name, city: Temp.city}
DELETE Temp 
RETURN node1, node2

我使用的语法链接如下:

  • 复制属性:https://neo4j.com/docs/cypher-manual/current/clauses/set/#set-copying-properties-between-nodes-and-relationships
  • 更改属性:https://neo4j.com/docs/cypher-manual/current/clauses/set/#set-setting-properties-using-map
英文:

This is the way I would write the query. You had the right structure, but there were a couple of syntax issues. The biggest one was that you need to use a SET clause when you create the Temp node. To answer your question whether we can avoid creating a temp node, I don't believe that's possible because you typically overwrite existing properties. And, if you don't overwrite them, you would have to retain/track the old properties to move over.

MATCH
  (node1 {id: 'id1'}),
  (node2 {id: 'ID1'})
CREATE (Temp:Student {id: node1.id})
 SET Temp = properties(node1) 
WITH node1, node2, Temp
 SET node1 += {name: node2.name, city: node2.city},
     node2 += {name: Temp.name, city: Temp.city}
DELETE Temp 
RETURN node1, node2

Links for syntax I used are here:

答案3

得分: 1

你应该使用 WITH 子句来存储临时值。假设两个节点都有 Student 标签,这个查询应该可以工作:

MATCH
  (n1:Student {id: 'id1'}),
  (n2:Student {id: 'ID1'})
WITH n1, n2, n1.name AS name1, n1.city AS city1
SET
  n1.name = n2.name, n1.city = n2.city,
  n2.name = name1, n2.city = city1
RETURN n1, n2
英文:

You should use the WITH clause to store temporary values. Assuming both nodes have the Student label, this query should work:

MATCH
  (n1:Student {id: 'id1'}),
  (n2:Student {id: 'ID1'})
WITH n1, n2, n1.name AS name1, n1.city AS city1
SET
  n1.name = n2.name, n1.city = n2.city,
  n2.name = name1, n2.city = city1
RETURN n1, n2

huangapple
  • 本文由 发表于 2023年6月27日 20:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564945.html
匿名

发表评论

匿名网友

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

确定