Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j nodes or relationship supports ttl?

I am learning neo4j , i want to know that is there any way that i can create a relationship or a node that will be delete automatically after a certain period of time.

like image 786
Govind Singh Avatar asked Feb 13 '15 11:02

Govind Singh


People also ask

What must Relationships in Neo4j have?

Relationships always has a direction (one direction). Relationships must have a type (one type) to define (classify) what type of relationship they are. Nodes and relationships can have properties (key-value pairs), which further describe them.

How does Neo4j store relationships?

Properties are stored as a linked list of property records, each holding a key and value and pointing to the next property. Each node and relationship references its first property record. The Nodes also reference the first relationship in its relationship chain. Each Relationship references its start and end node.

How do you create a relationship between two nodes in Neo4j?

To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. The created relationship is returned by the query.

Can a node have multiple labels Neo4j?

If you look closely, the labels column is in the form of an array, which means that a single node can have multiple labels. Also, with cypher projection, we can provide a virtual label, as shown in the query.


3 Answers

There's nothing that I know of like this. Neo4j is just a database like *SQL or MongoDB (though let me know if they can do something like this).

The best suggestion that I would have is to put a delete_after property (or something similar) on the relationships and then have a job which queries on a regular basis to clean them up. Note that you can't query for relationships directly (that is, nodes always need to be involved in your query) so depending on how big your database is, you may need to think through what sort of index you need. I'm a bit vague here because I don't know what your domain model would look like.

like image 59
Brian Underwood Avatar answered Oct 18 '22 16:10

Brian Underwood


As pointed out by @Scott in the comments, you can specify a TTL on nodes by using APOC as shown here. Append the following to your neo4j.conf:

apoc.ttl.enabled=true

Then you can either set the appropriate label and property yourself:

SET n:TTL
SET n.ttl = timestamp() + 3600

or utilize one of the following procedures:

// Expires in
CALL apoc.date.expire.in(node,time,'time-unit')

// Expires at
CALL apoc.date.expire(node,time,'time-unit')
like image 3
Daniel Rearden Avatar answered Oct 18 '22 16:10

Daniel Rearden


If you are like me and stumble to this article, this has recently been updated. Ref: https://neo4j.com/labs/apoc/4.3/overview/apoc.ttl/apoc.ttl.expireIn/

    Match(person:person {id: 100})
    CALL apoc.ttl.expireIn(person, 10,'s')
    Return person;
like image 2
Namrata Avatar answered Oct 18 '22 15:10

Namrata