Defining a path for node label?

Hi Everyone,

I am very new to graph databases and Neo4j so I really hope this type of question fits this Forum.

I have a simple graph where 3 nodes (Type 1, Type 2, Type 3) are connected to two of the same node through a [:HAS] relationship like this:

MATCH (windowtype:ComponentType{name:"Type 1"}),(biologicalprop:Properties{name:"Biological Properties"}) MERGE (windowtype)-[:HAS]->(biologicalprop)

MATCH (windowtype:ComponentType{name:"Type 1"}),(physicalprop:Properties{name:"Physical Properties"}) MERGE (windowtype)-[:HAS]->(physicalprop)

and for every other type, I just exchanged {name:"Type 1"} to {name:"Type 2"} and then {name:"Type 3"}

Then from this node
(physicalprop:Properties{name:"Physical Properties"})
I have a relationship to one more node
MATCH (physicalprop:Properties{name:"Physical Properties"}),(lifespans:PhysicalProperty{name:"Lifespans and Durability"}) MERGE (physicalprop)-[:CONSISTS_OF]->(lifespans)

I want the label of the node (lifespans:PhysicalProperty{name:"Lifespans and Durability"}) to be different for each Type.

The goal is to be able to query for the lifespans for the different types, fx. Type 1 lifespans=30, Type 2 lifespans=40, Type 3 lifespans=50. Which types have a lifespan of less than 45? And it should give Type 1 and Type 2.

Is there way to define node labels by paths or something similar?
Or do I need another approach?

I hope this makes sense.
I appreciate any help in advance!

You can use SET and UNSET to change the labels on nodes just as you would a property. For example:

MATCH (any:PhysicalProperty)
SET any:Type1
WHERE any.type = "type 1"
RETURN count(any)

I can't quite follow what you want. E.g. Does Type always determine Lifespan and vice versa?


One possibility is you can create a lifespan PROPERTY per node. I like this because it gives you more flexibility in case a particular Type has more than one lifespan. You could also have a PROPERTY of when the node was put in service, so you could make a calculation of time remaining for each node.
CREATE (n {lifespan:30})

(One way to think of Properties, is they are sort of like columns in a relational DB, except they aren't required and you can add and remove them at will.)

Then the lifespan isn't tied in with the type (which I think may be a better design.)

You can also have multiple labels on a node, so you can have:
CREATE (n:Thing:Lifespan30:Type1)

I don't know if "durability" is different from "lifespan", which is another consideration.

It really depends of what kinds of queries you plan to make. E.g. maybe lifespan can be shorten or lengthen based on other properties.

Can you give us more information/context please?

Hi David, Thank you so much for your reply!
Very helpful with the SET/UNSET command.

1 Like

Thanks a lot for your reply!
I did not know that I could set multiple labels as CREATE (n:Thing:Lifespan30:Type1) you suggest. I think I went in way over my head with what I wanted to create and should simplify it with the suggested labels.