Store a return value into another node

Hi all,

I have some return values which I like to store into a node.

//Cable Overview
MATCH(cables:Cables)
RETURN count(cables), sum(cables.Lengte)+sum(cables.Lengte_E)

//Get cable information V1.0
// Create a node
CREATE (cableInfo:CableInformation {nrOfCables:0,
lengthOfCables: 0,
lengthOfSpareCables: 0,
nrOfCablesLocal: 0,
nrOfCablesPulledMain: 0,
nrOfSpareCables: 0
}
)

How can I store the result of RETURN count(cables) into cableInfo.nrOfCables ?

The main goal is collecting data from Cables and store it in a separate table.

What is the best solution?

I tried the construction below:

//Get cable information V1.0
// Create a node
CREATE (cableInfo:CableInformation {nrOfCables:0,
lengthOfCables: 0,
lengthOfSpareCables: 0,
nrOfCablesLocal: 0,
nrOfCablesPulledMain: 0,
nrOfSpareCables: 0
}
)
WITH max(1) as dummy
MATCH (cables:Cables)
"test" = count(cables)
MATCH(cableInfo:CableInformation)
set cableInfo.nrOfCables = $test
return cableInfo

Got the error "Invalid input 'test': expected".

Tanks in advance

Hi r.kempers,

welcome to the Community forum!

You can match several (diverse) nodes (assuming they all already exist in the db) in one query and write the result on one of them e.g. like this:

MATCH (cables:Cables)
MATCH (cableInfo:CableInformation)
WITH count(cables) AS countCables, sum(cables.Lengte)+sum(cables.Lengte_E) AS sumLengte, cableInfo
SET cableInfo.nrOfCables = countCables, cableInfo.lengthOfCables = sumLengte

Regards,
Elena

Elena,
Thanks for your replay. Its help me a lot to understand Cypher even I'm a software engineer for many years.

One remark,
When I look for the WITH clause on the internet, for instance [Neo4j Manual, I missed the explanation why you should add "cableInfo" to the WITH clause. What I understand now "cableInfo" is essential in the WITH clause to make this query working.

Hi,

whenever you use a WITH statement you basically loose all variables that you had assigned previously. In this case: "cables" and "cableInfo" are variable names that you gave to the nodes that are found by the match and they get lost if you don't put them into the WITH statement which preserves the variable.

Regards,
Elena

and per WITH - Cypher Manual

It is important to note that WITH affects variables in scope. Any variables not included in the WITH clause are not carried over to the rest of the query. The wildcard * can be used to include all variables that are currently in scope.