Graph Database
Node
Relationship
(:beer:ipa { name: "Lervig Rye IPA"})
wget http://neo4j.com/artifact.php?name=neo4j-co...
tar xf neo4j-community-2.3.1-unix.tar.gz
cd neo4j-community-2.3.1-unix
bin/neo4j start
Done!
Neo4j with Node.js (using the Seraph library which I maintain and co/es6)
let co = require('co');
let seraph = require('seraph/co');
let db = seraph('http://localhost:7474');
co(function *() {
let jon = yield db.save({ name: 'Jon Packer' }, 'person');
let brik = yield db.save({ name: 'BRIK Videobase AS' }, 'company');
let rel = yield db.relate(jon, 'works_at', brik, { for: '4 years' });
return { jon, brik, rel }
}).then(function(output) {
console.log(output);
});
This time in Cypher.
CREATE (jon:person { name: 'Jon' })
-[:works_at { for: '4 years' }]->
(brik:company { name: 'BRIK Videobase AS' })
* looks even better when you don't need to split it over 3 lines!
SQL:
SELECT *
FROM beers
INNER JOIN breweries ON breweries.brewery_id = beers.beer_id
INNER JOIN beer_stock ON beer_stock.beer_id = beers.beer_id
INNER JOIN stores ON beer_stock.store_id = stores.store_id
WHERE beers.beer_title = 'Lervig Rye IPA'
AND stores.store_name = 'Bergen, Bergen Storsenter Vinmonopol'
Cypher:
MATCH (ipa:beer { title: 'Lervig Rye IPA' })<-[:brews]-(lervig:brewery),
ipa-[stock:in_stock]->(store:store { name: 'Bergen, Bergen Storsenter Vinmonopol' })
RETURN *
MATCH (ipa:beer { title: 'Lervig Rye IPA' })
identifier
label
predicate
MATCH (ipa:beer)
WHERE ipa.title = 'Lervig Rye IPA'
OR ipa.title = 'Lervig Galaxy IPA'
RETURN ipa
MATCH (:brewery)-[:brews]->(:beer)-[:brewed_in]->(:country)
relationship
directionality
MATCH (b:beer)-[stock:in_stock { quanitity: 25 }]-(s:store)
MATCH (beer:beer)-[:has_style]->(:style { name: 'India Pale Ale (IPA)' }),
beer-[stockLevel:in_stock]->(store:store)
WHERE store.name =~ 'Bergen.*'
AND beer.ratebeerWeightedAverage > 3.9
RETURN *
CREATE (beer:beer { title: 'I made this up' })
<-[:brews]-(:brewery { name: 'Monadic Ale' })
MERGE (beer:beer { title: 'I made this up' })
<-[:brews]-(:brewery { name: 'Monadic Ale' })
MATCH (monadic:brewery { name: 'Monadic Ale' })
MERGE monadic-[:brews]->(beer:beer { title: 'Katajanjoulu' })
CREATE UNIQUE (:brewery { name: 'Monadic Ale' })
-[:brews]->(beer:beer { title: 'Katajanjoulu' })
MATCH (veholt:brewery { name: 'Veholt Mikrobryggeri' })
-[:brews]->(beer:beer)
RETURN veholt, COLLECT(beer.title) as beers
Results in
[ { veholt: { name: 'Veholt Mikrobryggeri', id: 3385 },
beers: [ 'Veholt Humlehelvete Double IPA Originalen',
'Veholt Jimmy Red' ] } ]
If we didn't use COLLECT:
[ { veholt: { name: 'Veholt Mikrobryggeri', id: 3385 },
beer: 'Veholt Humlehelvete Double IPA Originalen' },
{ veholt: { name: 'Veholt Mikrobryggeri', id: 3385 },
beer: 'Veholt Jimmy Red' } ]