Neo4j - how to take a snapshot of everything in a shortcut

Im using neo4j for storing card and sensor information. Every time a card or gauge changes, I need to save a copy. I can submit a request and manually create the specified copy, but I'm wondering if it's possible to build a query like neo4j that will do this for me.

So far all I have come up with is a way to replicate the nodes in a given label:

match ( a:some_label { some_params }) with a create ( b:some_label ) set b=a,b.other_id=value;

      

This will allow me to put version and timestamp information on the given snapshot.

What it doesn't do is copy the edge information. Suggestions? Maybe a second (similar) query?

0


source to share


1 answer


If I understand you correctly, you are essentially trying to keep the state history of the node and the state of the incoming relationship. One way to do this is to tie the knots in reverse chronological order.

For example, suppose the nodes in the chain are tagged Some_label

and the relationship is of type SOME_TYPE

. The head node of the chain is always the current (last) node. If a Some_label

node is chronologically the earliest node in the chain, it will be related SOME_TYPE

to the previous version of node.

This is how you insert a new link and a node (with some properties) at the head of the chain. (To customize this example, I am assuming the first node in the chain is linked by some node to a label HeadRef

).



MATCH (x:HeadRef)-[r1:SOME_TYPE]->(a1:Some_label)
CREATE (x)-[r2:SOME_TYPE {x: "ghi"}]->(a2:Some_label {a:123, b: true})-[r:SOME_TYPE]->(a1)
SET r=r1
WITH r1
DELETE r1

      

Note that this approach is also much more efficient than storing your own property other_id

to link the nodes together. You should always use relationships instead - this is the DB graph path.

0


source







All Articles