Multiple queries with same MATCH statement

Hello,

I'm using driver for Neo4j database with this code:

As you can see, I create query and then I might or might not create other nodes BASED on some conditions and collections.
Problem is that every time I have to match that first node again and again.

Is there some way I can temporally store the first node into some variable, so I dont have to search for that?

Your first statement could do a return id(n) as id.

In your follow-up statements you'd use match (n) where id(n)=$i .... and pass in {id: <id from 1st query>} as a parameter. This does a simple lookup by id which is basically a seek operation - extremely fast.

Another idea would be to run all your stuff in one single statement, cypher has a foreach / unwind.