I know that nodes and relationships have an integral value that identifies them. Is the same true of labels and/or properties? Is it sufficient to identify a node labeling by giving a node id and a string? That is, is it possible to assign the same label to the same node/relationship more than once?
-
Can you clarify? Maybe give an example of what you are trying to achieve. Technically I suspect the answer the the question title is no, the "is it sufficient" is yes and the "assign same label" is why? – JohnMark13 Oct 15 '14 at 14:15
-
@JohnMark13 trying to understand the data model and the documentation is silent on this point – jameshfisher Oct 15 '14 at 14:23
-
But can you edit the question and elaborate on exactly what you want to understand or achieve? Do you mean the internal data model like here? http://www.neo4j.org/develop/internals – JohnMark13 Oct 15 '14 at 14:32
-
@JohnMark13 I'm trying to understand the data model. I don't care very much about an "internal" data model; I care about the external data model exposed to the user. For nodes and relationships, this includes an id value. I want to know if labels and properties also have such ids. – jameshfisher Oct 15 '14 at 15:45
3 Answers
All nodes and relationships have IDs and can be looked up by their IDs. You can do it like this:
MATCH (n) WHERE id(n) = 5 RETURN n;
IDs should be thought of as an internal implementation detail; don't rely on them to have any particular value or to be consistent or ordered, just rely on them to uniquely identify each node.
In general, IMHO it's good practice to assign your own meaningful identifier to nodes, probably indexed, that you can use to find your nodes, in the same way you'd assign a primary key to a relational database record.
It is possible to assign a label to more than one node; labels should be thought of more as classes of nodes, sort of like entities in an ERD. Typically labels would be things like Person, Company, Job, etc. Labels don't have much to do with identifiers though.

- 1
- 1

- 17,634
- 4
- 52
- 86
-
Perhaps I wasn't clear -- I understand that nodes and relationships have IDs. That's not an implementation detail; it's a fact exposed in the interface, e.g. Cypher. I want to know whether the same is true of labels and/or properties -- do they also have ids exposed? – jameshfisher Oct 15 '14 at 15:47
-
Well, answering that is pretty easy - no, labels don't have IDs that you can access, and neither do properties. – FrobberOfBits Oct 15 '14 at 16:04
-
1Also - yes, IDs are exposed in cypher, but see this other question - they should be thought of as an implementation detail because usually almost any form of reliance on them tends to lead you down a path that isn't toward a good design. http://stackoverflow.com/questions/9051442/node-identifiers-in-neo4j – FrobberOfBits Oct 15 '14 at 16:06
-
Do labels and properties have an id value in Neo4j?
No
Is it sufficient to identify a node labeling by giving a node id and a string?
If by this you mean that you have the node id and a label value in a String variable then yes, it is. It is also sufficient to just use the ID.
MATCH (n) WHERE ID(n) = 1234 RETURN n
Better:
MATCH (n:YourLabel) WHERE ID(n) = 1234 RETURN n
As FrobberOfBits intimated it is also preferable to ignore (withint reason) the internal IDs that Neo attaches to Nodes/Relationships in your external interactions. This is in part to do with Neo recycling it's internal identifiers (So if you create a Node it gets assigned ID 1, delete that Node, the next created Node could be assigned ID 1), and inpart to do with exposing the internals of the system. Instead you should probably attach meaningful identifiers or UUIDS where required.
Using your own identifier:
MATCH (n:YourLabel{uid:1234}) RETURN n
To make lookups fast, index them:
CREATE INDEX ON :YourLabel(uid)
Is it possible to assign the same label to the same node/relationship more than once?
Nodes can have as many labels as you want to assign them (including none), which is handy when you want to maintain a hierarchy of Node "types". Having a label makes them faster to lookup as Neo has a hint of where to start.
Relationships can only have a single type and that type is immutable. i.e If you want to change from type HAS_A
to HAD_A
you cannot just change the type, you must delete and re-add the relationship.
A node can be related to another node as many times as you want, using the same relationship type or different relationship types. Performancewise it is better to have different relationship types than to use properties on relationships as the lookup is faster.
CREATE (p:Person{name:"Dave"}), (m:Pet), (d:Pet),
(p)-[:HAS_PET{type:"Cat"}]->(m),
(p)-[:HAS_PET{type:"Dog"}]->(d)
Is fine, as is:
REATE (p:Person{name:"Dave"}), (m:Pet), (d:Pet),
(p)-[:HAS_CAT]->(m),
(p)-[:HAS_DOG]->(d)
Which is now faster if you wanted to do a query for matching all dogs. If Dave's dog gets reassigned you cannot do:
MATCH (p:Person{name:"Dave"})-[rel:HAS_DOG]->()
SET rel:HAS_CAT
But you could do:
MATCH (p:Person{name:"Dave"})<-[rel:HAS_PET{type:"Dog"}]-()
SET rel.type = "Cat"
But I've probably missed the point of the multiple-assignment question.

- 3,709
- 1
- 15
- 26
Not sure what you're aiming for:
yes, property-names, rel-types and labels have internal id's they are not stored as strings
you can identify nodes by label + property + value, one if you have a uniqueness constraint otherwise multiple
- you can identify nodes by label (many)

- 41,339
- 3
- 57
- 80