Thursday, October 29, 2020

Neo4j Query Language - Cypher

MATCH (p:Person { name:"Homer Flinstone" })

RETURN p


This Cypher statement returns a "Person" node where the name property is "Homer Flinstone".



Neo4j doesn't store its data in tables like the relational database model. It's all in nodes and relationships. So the Cypher query above is querying nodes, their labels, and their properties. The SQL example on the other hand, is querying tables, rows, and columns.



Neo4j is a NoSQL DBMS, in that it doesn't use the relational model and it doesn't use SQL.



ASCII-Art Syntax


Cypher uses ASCII-Art to represent patterns. This is a handy thing to remember when first learning the language. If you forget how to write something, just visualise how the graph will look and it should help.


(a)-[:KNOWS]->(b)


Nodes are represented by parentheses, which look like circles. Like this: (node)

Relationships are represented by arrows. Like this: ->

Information about a relationship can be inserted between square brackets. Like this: [:KNOWS]



Defining the Data

Nodes usually have labels. Examples could include "Person", "User", "Actor", "Employee", "Customer".

Nodes usually have properties. Properties provide extra information about the node. Examples could include "Name", "Age", "Born", etc

Relationships can also have properties.

Relationships usually have a type (this is basically like a node's label). Examples could include "KNOWS", "LIKES", "WORKS_FOR", "PURCHASED", etc.



So looking at the above example again:



MATCH (p:Person { name:"Homer Flinstone" })

RETURN p

We can see that:


The node is surrounded by parentheses ().

Person is the node's label.

name is a property of the node.




References:

https://www.quackit.com/neo4j/tutorial/neo4j_query_language_cypher.cfm




No comments:

Post a Comment