Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are labels in graph database

Tags:

label

neo4j

I'm new to graph databases and currently experimenting with neo4j. Can someone please help me understand:

1) what are labels exactly? 2) how/where they are used? 3) Why do we need them? Can we work without them?

I've read about labels but i'm not able to grasp this concept.

Thanks.

like image 963
dk007 Avatar asked Sep 13 '25 09:09

dk007


1 Answers

As you can read in official doc, labels represent a kind of class, or better: a type, of a node.

A label is a named graph construct that is used to group nodes into sets; all nodes labeled with the same label belongs to the same set. Many database queries can work with these sets instead of the whole graph, making queries easier to write and more efficient to execute. A node may be labeled with any number of labels, including none, making labels an optional addition to the graph.

Labels are used when defining constraints and adding indexes for properties (see Schema).

An example would be a label named User that you label all your nodes representing users with. With that in place, you can ask Neo4j to perform operations only on your user nodes, such as finding all users with a given name.

However, you can use labels for much more. For instance, since labels can be added and removed during runtime, they can be used to mark temporary states for your nodes. You might create an Offline label for phones that are offline, a Happy label for happy pets, and so on.

It's important to say that a node can have multiple labels. For example, the node representing Benedict Cumberbatch, could be labeled as: Person,Man,Actor and British.

You can query nodes by labels. It means that the Benedict Cumberbatch's node belongs to each one of those sets and it will be returned in each resultsets of the following queries:

MATCH (p:Person) return p
MATCH (p:Man) return p
MATCH (p:Actor) return p 
MATCH (p:British) return p 

Labels are not mandatory but using them is considered a best practice to categorize your data and get them by types.

like image 139
floatingpurr Avatar answered Sep 16 '25 09:09

floatingpurr