Skip to content
Related Articles

Related Articles

Data Model In Neo4j
  • Last Updated : 18 Aug, 2020

The data model in Neo4j organizes data using the concepts of nodes and relationships. Both nodes and relationships can have properties, which store the data items associated with nodes and relationships. 

Nodes can have labels:  

  • A node can have zero, one, or several labels.
  • The nodes that have the same label are grouped into a collection that identifies a subset of the nodes in the database graph for querying purposes.

Relationships are directed, each relationship has a start node and end node as well as a relationship type, which serves a similar role to a node label by identifying similar relationships that have the same relationship type Properties can be specified via a map pattern, which is made of one or more “name : value” pairs enclosed in curly brackets. 

Example: (Lname: ‘Sharma’, Fname: ‘Nitin’, Minit : ‘B’). 

The Neo4j graph data model resembles how data is represented in the ER and EER models over the conventional graph thoery.



Creation of nodes in Neo4j: Creating nodes for a COMPANY:

CREATE (e1: EMPLOYEE, {Empid: ‘1’, Lname: ‘Sharma’, Fname: ‘Nitin’, Minit: ‘B’})
CREATE (e2: EMPLOYEE, {Empid: ‘2’, Lname: ‘Rao’. Fname: ‘Rupesh’})
CREATE (e3: EMPLOYEE, {Empid: ‘3’, Lname: ‘Dave’, Fname: ‘Gopal’))
CREATE (e4: EMPLOYEE, {Empid: ‘4’, Lname: ‘Ojha’, Fname: ‘Baibhav’, Minit: ‘S’})

CREATE (d1: DEPARTMENT, {Dno: ‘1’, Dname: ‘Business’})
CREATE (d2: DEPARTMENT. {Dno: ‘2’, Dname: ‘Developer’))

CREATE (p1: PROJECT, {Pno: ‘8’, Pname: ‘WebDev’})
CREATE (p2: PROJECT, {PNo: ‘2’, Pname: ‘AppDev’))
CREATE (p3: PROJECT, {Pno: ‘5’, Pnarne: ‘APIDev’})

CREATE (loc1: LOCATION, {Name: ‘Noida’})
CREATE (loc2: LOCATION, {Name: ‘Hyderabad’})
CREATE (loc3: LOCATION, {Name: ‘Bengaluru’})
CREATE (loc4: LOCATION, {Name: ‘Chennai’})

Creation of relationships in Neo4j: Creating relationships for a COMPANY:

CREATE (e1)- [: WorksFor]-> (d1)
CREATE (e3)- [: WorksFor]-> (d2)

CREATE (d1)- [: Manager ] -> (e2)
CREATE (d2)- [: Manager ] -> (e4)

CREATE (d1)- [: LocatedIn ]-> (loc1)
CREATE (d1)- [: LocatedIn ]-> (loc3)
CREATE (d1)- [: LocatedIn ]-> (loc4)
CREATE (d2)- [: LocatedIn ]-> (loc2)

CREATE (e1)- [: WorksOn, {Hours: ‘28.5’}] -> (p1)
CREATE (e1)- [: WorksOn, {Hours: ‘7.5’}] -> (p2)
CREATE (e2)- [: WorksOn, {Hours: ‘15.0’}] -> (p1)
CREATE (e2)- [: WorksOn, {Hours: ‘15.0’}] -> (p2)
CREATE (e2)- [: WorksOn, {Hours: ‘10.0’}] -> (p3)
CREATE (e2) -[: WorksOn, {Hours: ‘10.0’}] -> (p2)

Features of Neo4j Data Model:

  • Labels and properties: A node label can be declared/specified when a node is created.
    • It is also possible to create nodes without any labels.
  • Relationships and relationship types:->” specifies the direction of the relationship.
    • The relationship can be traversed in either direction.
  • Paths: A path specifies a traversal of part of the graph. Typically it is used as part of a query to specify a pattern, where the query will retrieve from the graph data that matches the pattern.
    • A path is typically specified by a start node, followed by one or more relationships, leading to one or more end nodes that satisfy the pattern.
  • Optional Schema:  Graphs can be created and used without a schema(Optional).
    • The main features related to schema creation involve creating indexes and constraints based on the labels and properties.
  • Indexing and node identifiers: The Neo4j system creates an internal unique system-defined identifier for each node, when a node is created. To retrieve individual nodes using other properties of the nodes efficiently, the user can create indexes for the collection of nodes that have a particular label.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :