Reputation: 3
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.
Upvotes: 0
Views: 720
Reputation: 30397
The problem is with this line:
WITH collect(n) AS nodes
You've matched to all :OUTPT nodes with a sequence number within 1-31, but then you aggregate them into a single large collection, then merge them into a single node.
If you want to collect the nodes according to the sequence number, then the sequence number (in your case, cnt
) needs to be the grouping key of the aggregation:
WITH cnt, collect(n) AS nodes
That will get you a row per distinct cnt
value, with the list of nodes with the same count on the associated row.
Because Cypher operations execute per row, your APOC refactor call will execute per row. Because each row is associated with a different cnt value, and each has a different list, you will be performing the refactoring for each list separately.
The output will be one row per cnt value, with a single node per row (as a result of merging all the nodes in that row's list into a single node).
Upvotes: 1