How to write the result of the shortestPath straight to the database?

When I run the following query, it extracts the path between nodes.

//ShortestPath planning

match (g:Id1 {start_marker:1}),
      (c:Id2 {break_point:1}),
      p = shortestPath((g)-[*]->(c))
return p

Extracted path from the graph:

Then I extracted that result using a procedure apoc.export.cypher.query, and when I uploaded the data back into the database, the result was following:

And that is exactly what I wanted.


How can I write that result straight into the database within my original query?

A shortest path from any element A to any other node B in the database is already written in the database; the path is already there.

Now, given your path, what you could do is iterate through the relationships and set some property on them indicating "follow this relationship for shortest path from A to B" if you wanted to. For that, I'd use the rels() function on the path p object that you have.

For example, something like this:

match (g:Id1 {start_marker:1}),
      (c:Id2 {break_point:1}),
      p = shortestPath((g)-[*]->(c))
WITH relationships(p) as relsInShortestPath, g, c
UNWIND relsInShortestPath as singleRel
SET singleRel.shortestPathFrom=id(g), singleRel.shortestPathTo=id(c)
RETURN count(singleRel)