Cypher (using bolt) multiple unrelated queries at once - match and modify (set property) of multiple nodes in single query

血红的双手。 提交于 2019-12-13 05:55:41

问题


I have nodes of 5 different labels, say, A, B, C, D, E. I need to match a node of each type based on some property and update some other property of the matched node.

The query works perfectly fine if i execute 5 different match and set queries, one for each label type.

However, when I try to do everything in one single query, the property seems to get updated multiple times. The property I am updating is an array (the same value gets added to the array multiple times). The following is the query I am trying to execute:

match (a:A {a: "abc"}) 
match (A {a: "abc"})-[RELATED_TO*]-(b:B {b:"def"}) 
match (c:C {c: "lmn"}) 
match (d:D {d: "pqr"}) 
match (e:E {e: "xyz"}) 
set a.prop=a.prop+"123" 
set b.prop=b.prop+"123" 
set c.prop=c.prop+"123" 
set d.prop=d.prop+"123" 
set e.prop=e.prop+"123"

The issue is that when I execute the above query the property array prop gets 123 added multiple times. Is it because the results of my match statement are multiple and the same set statements are run that many times. If that is the case, can someone suggest me the best way to do this in a single query.

The reason I want to do this in a single query is because I am sending cypher queries from a remote application using Bolt protocol, and I do not want to send 5 different queries.

[EDIT : Adding a simple example]

Consider the following simple arrangement of all 5 nodes in the DB:

When I run the above query on this set up, and assuming the property prop was an empty array before, the following is the property value after running the query:

A.prop = ["123", "123", "123", "123", "123", "123", "123", "123"]  // added 8 times
B.prop = ["123", "123", "123", "123", "123", "123", "123", "123"]  // added 8 times
C.prop = ["123", "123", "123", "123"]  // added 4 times
D.prop = ["123", "123", "123", "123"]  // added 4 times
E.prop = ["123", "123", "123", "123"]  // added 4 times

The desired outcome however should be:

A.prop = ["123"]  // added 1 time
B.prop = ["123"]  // added 1 time
C.prop = ["123"]  // added 1 time
D.prop = ["123"]  // added 1 time
E.prop = ["123"]  // added 1 time

[Edit 2] I found a somewhat similar question here but not a satisfying answer. I believe sending multiple unrelated queries at once from a remote application would be better than sending them one by one (from an application point of view)

来源:https://stackoverflow.com/questions/38112043/cypher-using-bolt-multiple-unrelated-queries-at-once-match-and-modify-set-p

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