Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Difference between revisions of "Neo4j:Basics - Introduction"

From NoSQLZoo
Jump to: navigation, search
m
Line 52: Line 52:
 
</div>
 
</div>
 
<div class="q" data-lang="neo4j">
 
<div class="q" data-lang="neo4j">
 +
'''WHERE _ IN''' can be used to check against a list of values.
 
<p class="strong">Find the Greek names of the 'Big Three' ('Zeus', 'Poseidon', 'Hades')</p>
 
<p class="strong">Find the Greek names of the 'Big Three' ('Zeus', 'Poseidon', 'Hades')</p>
 
<pre class="def"><nowiki>
 
<pre class="def"><nowiki>
Line 60: Line 61:
 
<pre class="ans"><nowiki>MATCH (g:God) WHERE g.name IN ['Zeus', 'Poseidon', 'Hades'] RETURN g.name, g.greek;</nowiki></pre>
 
<pre class="ans"><nowiki>MATCH (g:God) WHERE g.name IN ['Zeus', 'Poseidon', 'Hades'] RETURN g.name, g.greek;</nowiki></pre>
 
</div>
 
</div>
 
+
<div class="q" data-lang="neo4j">
 
+
Predictably, '''WHERE NOT _ IN''' performs the inverse of '''IN'''
Further [[Neo4j:MATCH_Examples| MATCH Examples]] can be found here.
+
<p class="strong">Find the names of the Olympians, excluding Apollo and Hestia</p>
 +
<pre class="def"><nowiki>
 +
MATCH (o:Olympian)
 +
RETURN o.name;
 +
</nowiki></pre>
 +
<pre class="ans"><nowiki>MATCH (o:Olympian) WHERE NOT o.name IN ['Apollo', 'Hestia'] RETURN o.name;</nowiki></pre>
 +
</div>
 +
Further examples can be found here: [[Neo4j:MATCH_Examples| MATCH Examples]].<br/>
 +
Once comfortable with these questions, move onto [[Neo4j:MATCH_Basics_-_Relationships| MATCH Basics - Relationships]].

Revision as of 17:29, 13 June 2018

Working with the greek_gods family tree graph

This tutorial introduces NoSQL using Neo4j. We will be using the RETURN, MATCH, and WHERE commands on the greek_gods collection which contains a family tree of around 130 individuals.

For anyone wishing to use this data-set on their own machines, the Cypher file used to build it is available here: *TBA*.

Introduction to Graphs and the 'Graph Property Model'

You've likely heard of graphs before. In mathematics a graph is the term for a set of vertices and edges, here the terms nodes and relationships are used instead.

In Neo4j graphs follow the "Property Graph Model". A property is just a key-value pair, such as name: 'Alice' or age: 27. Users who have used document-oriented databases such as MongoDB will find this concept very similar.

A node is a simple entity that contains any amount of properties, and they can be labeled as belonging to a certain set. For example, in a database of celebrities Stephen Fry could be labeled as a :Actor. There's no limit to the amount of labels that can apply to a node, the celebrity Hugh Laurie could be simultaneously labeled as an :Actor, :Musician, and :Author.

In the property graph model, a relationship is a connection between exactly two nodes, though there's not limit to the amount of relationships a node made have. A relationship always has both a name and a direction (start node and end node), and like a node it can also have properties. Bidirectional relationships are a little trickier, and are out-width the scope of this tutorial: Modelling Bidirectional Relationships

This remainder of this tutorial will use the Cypher query language to introduce some basic concepts.


RETURN

The RETURN clause determines what is included in the query result set. The following operation returns some numbers, strings, and lists added together.

RETURN 2 + 2, "A" + "B", [1, 2, 3] + [4];

MATCH and WHERE

The MATCH clause is used to search for data that meets a pattern you define.
The WHERE clause is used to restrict these patterns.

Find a node, n, with the label ':God' and a name property equal to 'Zeus'.

MATCH (n)
WHERE n:God AND n.name = 'Zeus'
RETURN n;
MATCH (g) WHERE g:God AND g.name = 'Zeus' RETURN g; 

In most scenarios, WHERE and MATCH can be combined. e.g. using the previous example:

MATCH (g:God {name: 'Zeus'})
RETURN g;

This query currently gets Zeus' names.

Find Hera's English and Greek names.

MATCH (g:God {name: "Zeus"}) 
RETURN g.name, g.greek;
MATCH (g:God {name: "Zeus"}) RETURN g.name, g.greek;

WHERE _ IN can be used to check against a list of values.

Find the Greek names of the 'Big Three' ('Zeus', 'Poseidon', 'Hades')

MATCH (g:God) 
WHERE g.name IN ['Hera', 'Demeter'] 
RETURN g.name, g.greek;
MATCH (g:God) WHERE g.name IN ['Zeus', 'Poseidon', 'Hades'] RETURN g.name, g.greek;

Predictably, WHERE NOT _ IN performs the inverse of IN

Find the names of the Olympians, excluding Apollo and Hestia

MATCH (o:Olympian)
RETURN o.name;
MATCH (o:Olympian) WHERE NOT o.name IN ['Apollo', 'Hestia'] RETURN o.name;

Further examples can be found here: MATCH Examples.
Once comfortable with these questions, move onto MATCH Basics - Relationships.