How to save the result of a query (sub graph) in cypher

Here is a solution that I used, Collected the ids of nodes, created a node and added the ids list as a property. 

Step: 1
match(n)-[r]->(m) where not m.name in ['JANSSEN','PFIZER\BIONTECH','ANOFI PASTEUR','Hospital','Recovered'] and not n.name in ['JANSSEN','PFIZER\BIONTECH','ANOFI PASTEUR','Hospital','Recovered'] 
with collect(distinct id(n)) as n1, collect(distinct id(m)) as m1
with apoc.coll.union(n1, m1) as res1
with apoc.coll.sort(res1) as final

merge (a:SubGraph {name: "xyz", ids: final})

Step 2:

match (c:SubGraph) where c.name = "xyz"
with c.ids as sub1
unwind sub1 as sub2
with collect(sub2) as s1
match(x) where id(x) in s1
return x

Displays the subgraph. Another option is to use apoc.export.json.query to create a json file.

1 Like