Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-08-2021 10:53 AM
I am very new to Neo4j, so this is probably a simple question.
I have several hundred nodes with a property "seq" (for sequence). This number basically represents the day of the month. So all of these several hundred nodes have a seq property between 1 and 31. I want to combine all the nodes with the same seq into a single node - so that all the nodes with seq = 1 are combined into a "January 1" node. All nodes with seq =2 are combined into a "January 2" node, etc. I have a property of "pat_id" that will be combined into an array from all the merged noes for a day.
Here is my code:
WITH range(1,31) as counts
UNWIND counts AS cnt
MATCH (n:OUTPT {seq:cnt})
WITH collect(n) AS nodes
CALL apoc.refactor.mergeNodes(nodes, {properties: {
pat_id:'combine',
seq:'discard'}, mergeRels:true})
YIELD node
RETURN node
I initially tried to do this with a FOREACH loop, but I can't do a MATCH inside a FOREACH.
I have been doing an UNWIND, but it is only merging the nodes with the first value (seq = 1). I assume this is because the RETURN statement ends the loop. But when I remove the RETURN statement, I get this error:
Query cannot conclude with CALL (must be RETURN or an update clause) (line 5, column 1 (offset: 99))
"CALL apoc.refactor.mergeNodes(nodes, {properties: {"
Any help would be appreciated.
Solved! Go to Solution.
04-12-2021 02:23 PM
Hello @michael.temple1 and welcome to the Neo4j community
You were very close, you missed the cnt
in the WITH
clause:
WITH range(1,31) as counts
UNWIND counts AS cnt
MATCH (n:OUTPT {seq:cnt})
WITH cnt, collect(n) AS nodes
CALL apoc.refactor.mergeNodes(nodes, {properties: {
pat_id:'combine',
seq:'discard'
}, mergeRels:true})
YIELD node
RETURN node
Regards,
Cobra
04-12-2021 02:23 PM
Hello @michael.temple1 and welcome to the Neo4j community
You were very close, you missed the cnt
in the WITH
clause:
WITH range(1,31) as counts
UNWIND counts AS cnt
MATCH (n:OUTPT {seq:cnt})
WITH cnt, collect(n) AS nodes
CALL apoc.refactor.mergeNodes(nodes, {properties: {
pat_id:'combine',
seq:'discard'
}, mergeRels:true})
YIELD node
RETURN node
Regards,
Cobra
04-12-2021 02:29 PM
and although no a significant benefit but
WITH range(1,31) as counts
UNWIND counts AS cnt
could be replaced by
UNWIND range(1,31) AS cnt
04-12-2021 06:06 PM
Thank you Cobra - this works as I want to to.
Quick follow-up. I don't understand why I need the 'cnt' in the WITH statement. If I run the following code:
WITH range(1,31) as counts
UNWIND counts AS cnt
RETURN cnt
I get 31 rows back. I thought Neo4j operated on a row by row basis. So I don't quite understand why it combines all 31 rows back into a single node if I don't have 'cnt' in the WITH statement.
Also, why can you not use MATCH in a FOREACH statement? Is there something 'under the hood' in Neo4j that prevents this? Being able to MATCH in a FOREACH seems like it would make something like this much more straight-forward and easier to figure out.
Once again, thanks for your help. I really appreciate it.
04-12-2021 11:46 PM
Hello @michael.temple1
You must put the cnt
variable because of the collect()
that is an aggregation function. When you use an aggregation function, you must think by what you want to aggregate.
For FOREACH(), you can only do any of these updating commands: SET
, REMOVE
, CREATE
, MERGE
, DELETE
and FOREACH
.
Regards,
Cobta
All the sessions of the conference are now available online