CREATE (p:Person);
CREATE (p:Person
{
name: "Manjit",
age: 22
}
);
CREATEclause to create data.()parenthesis to indicate a node.p:Persona variablepand labelPersonfor new node.{}brackets to add properties to the node.
CREATEclause can create many nodes and relationships at once.CREATEorMERGEfor writing data.
CREATE (mn:Person
{
name: "Manjit",
address: "Sundhara",
learn: "Gaming"
}
),
(sd:Person
{
name: "Sandesh",
address: "Sunakothi",
hobby: "Dancing"
}
),
(kl:Person
{
name: "Kushal",
address: "Kirtipur",
hobby: "Singing"
}
),
(rl:Person
{
name: "Rahil",
address: "Kanibahal",
hobby: "Gaming"
}
),
(mn)-[:KNOWS {since: 2017}]->(sd),
(mn)-[:KNOWS {rating: 5}]-> (kl),
(kl)-[:KNOWS]->(rl),
(kl)-[:KNOWS]->(sd),
(sd)-[:KNOWS]->(rl);
[:KNOWS]is a relationship type.[:KNOWS {since: 2017}]is relationship properties.->determines relationship direction.
MATCHclause is for reading data. TheMATCHclause describes a pattern of graph data. Neo4j will collect all paths within the graph which match this pattern.
MATCH (p:Person) WHERE p.name = "Manjit" RETURN p;
MATCHclause to specify a pattern of nodes and relationships.(p:Person)a single node pattern with labelPersonwhich will assign matches to variablep.WHEREclause to constrain the results.p.name = "Manjit"compares name property to the value "Manjit"RETURNclause used to request particular results.
MATCH (p:Person)-[:KNOWS]-(friends) WHERE p.name = "Manjit" RETURN p, friends;
(p:Person)-[:KNOWS]-(friends)is the pattern.-[:KNOWS]-matches "KNOWS" relationship in either direction.(friends)will be bound to Manjit's friends.
Pattern matching can be used to make recommendation. Manjit is learning to play games, so he may want to find a new friend who already does.
MATCH (mn:Person)-[:KNOWS]-()-[:KNOWS]-(gamer) WHERE mn.name = "Manjit" AND gamer.hobby = "Gaming" RETURN DISTINCT gamer;
()empty parenthesis to ignore these nodes.DISTINCTbecause more than one path will match the pattern.gamerwill contain Rahil, a friend of a friend who is a gamer
The
SETclause updates properties on nodes and relationships
MATCH (p:Person)-[w:LOVES]->(girl) WHERE p.name="Sandesh"
SET w.relationship = "2 years";
The
WHEREclause imposes conditions on the data within a potentially matching path, filtering the result set of aMATCH.MATCHdescribes the structure, and WHERE specifies the content of a query.
MATCH (p:Person)-[:CODES]->(language)
WHERE p.name = "Sandesh"
RETURN language.name;
The
RETURNclause defines what to include in a query result set, specified as a comma separated list of expressions.
MATCH (p:Person)-[:CODES]->(language)
RETURN p.name AS name, collect(language.name) as Languages;
The
DELETEclause is used to delete nodes and relationships identified within aMATCHclause, possibly qualified by aWHERE. Remember that you can not delete a node without also deleting relationships that start or end on said node.
MATCH (n)-[r]-() WHERE n.name = "Manjit" DELETE r;
The query above delete all the relationships with Manjit's friend's.
By adding DESC or ASC after the variable to sort on, the sort will be done in reverse order for DESC and Serial format for ASC (Ascending order).
MATCH (n)
RETURN n.name, n.age
ORDER BY n.name DESC
By using
SKIP, the result set will get trimmed based on value.
MATCH (n)
RETURN n.name
ORDER BY n.name
SKIP 1
LIMIT 2
The
DETACH DELETEclause is used to delete all nodes and relationships.
MATCH (n) DETACH DELETE n
The
FOREACHclause is used to update data within a collection.
MATCH p = (ups)<-[DEPENDS_ON]-(device) WHERE ups.id='EPS-7001'
FOREACH (n IN nodes(p) | SET n.available = FALSE )
The
WITHclause allows queries to be chained together, piping the results from one to be used as starting points or criteria in the next.
WITHallows you to pass on data from one part of the query to the next. Whatever you list inWITHwill be available in the next query part.
That means you can chain query parts where one computes some data and the next query part can use that computed data. In your case it is what GROUP BY and HAVING would be in SQL but
WITHis much more powerful than that.
MATCH (n)
WITH n
ORDER BY n.name DESC LIMIT 3
RETURN collect(n.name)
MATCH (david { name: 'David' })--(otherPerson)-->()
WITH otherPerson, count(*) AS foaf
WHERE foaf > 1
RETURN otherPerson.name
LOAD CSVclause loads csv from file located at given URL
LOAD CSV FROM "http://data.neo4j.com/examples/person.csv" AS line
MERGE (n:Person {id: toInteger(line[0])})
SET n.name = line[1]
RETURN n
A database index is a redundant copy of information in the database for the purpose of making retrieving said data more efficient. This comes at the cost of additional storage space and slower writes, so deciding what to index and what not to index is an important and often non-trivial task. The
CREATE INDEX ONclause will create and populate an index on a property for all nodes that have a label.
Two types single-property index and composite index.
CREATE INDEX ON :Person(name);
CREATE INDEX ON :Person(firstname,lastname);
DROP index
DROP INDEX ON :Person(name);
DROP INDEX ON :Person(firstname,lastname);
List all indexes
CALL db.indexes;
CALL db.indexes YIELD description, label, properties;
Combines the result of multiple queries into a single result set. Duplicates are removed.
MATCH (n:Actor)
RETURN n.name AS name
UNION
MATCH (n:Movie)
RETURN n.title AS name
Combines the result of multiple queries into a single result set. Duplicates are retained.
MATCH (n:Actor)
RETURN n.name AS name
UNION ALL MATCH (n:Movie)
RETURN n.title AS name
Constraints are used to make sure that the data adheres to the rules of the domain. For example: "If a node has a label of Actor and a property of name, then the value of name must be unique among all nodes that have the Actor label".
CREATE CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE
Drop constraints
DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE
To create a constraint that ensures that all nodes with a certain label have a certain property, use the
ASSERT exists(variable.propertyName)syntax.
CREATE CONSTRAINT ON (book:Book) ASSERT exists(book.isbn)
After enforcing this constraint
CREATE (book:Book { isbn: '1449356265', title: 'Graph Databases' })will be executed butCREATE (book:Book { title: 'Graph Databases' })will not execute and return error message "Node(0) with labelBookmust have the propertyisbn"
drop existence constraint
DROP CONSTRAINT ON (book:Book) ASSERT exists(book.isbn)
To create a constraint that makes sure that all relationships with a certain type have a certain property, use the
ASSERT exists(variable.propertyName)syntax.
CREATE CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day)
Above query will enforce the relationship constraint where
DROP CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day)is fired successfully but thisCREATE (user:User)-[like:LIKED]->(book:Book)will give error "Relationship(0) with typeLIKEDmust have the propertyday".
To create a Node Key ensuring that all nodes with a particular label have a set of defined properties whose combined value is unique, and where all properties in the set are present, use the
ASSERT (variable.propertyName_1, …, variable.propertyName_n)IS NODE KEY syntax.
CREATE CONSTRAINT ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEY
Drop
DROP CONSTRAINT ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEY
DROP CONSTRAINT ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEYwill fire andCREATE (p:Person { firstname: 'Jane', age: 34 })will fail and give error "Node(0) with labelPersonmust have the propertiesfirstname, surname"
We can inspect our database to find out what constraints are defined.
CALL db.constraints
Matching the start of a string. The start of strings can be matched using
STARTS WITH. The matching is case-sensitive.
MATCH (p:Person)
WHERE p.name STARTS WITH 'Man'
RETURN p.name
The end of strings can be matched using
ENDS WITH. The matching is case-sensitive.
MATCH (p:Person)
WHERE p.name ENDS WITH 'jit'
RETURN p.name
The occurrence of a string within a string can be matched using
CONTAINS. The matching is case-sensitive.
MATCH (p:Person)
WHERE p.name CONTAINS 'anj'
RETURN p.name
Understand how your query works by prepending
EXPLAINorPROFILE
PROFILE MATCH (mn:Person)-[:KNOWS]-()-[:KNOWS]-(gamer) WHERE mn.name = "Manjit" AND gamer.hobby = "Gaming" RETURN DISTINCT gamer;
Learn about queries using
:help cypher
-
A graph database can store any kind of data using a few simple concepts:
-
The simplest graph has just a single node with some named values called Properties.
-
Nodes can be grouped together by applying a Label to each member. Here, the label for grouping is Person, as both of them are Person type. In relational database their are Tables which store specific values instead of Labels. Based on JSON:
[ Person{ name:"Manjit Shakya" age: 22 }, Person{ name:"Sandesh Maharjan" age: 21 } ] -
A node can have zero or more labels
-
Labels do not have any properties
-
Similar bodes can have different properties
-
Properties can be strings, numbers, or booleans
-
Neo4j can store billions of nodes.
-
The real power of Neo4j is in connected data. To associate any two nodes, add a Relationship which describes how the records are related.
-
Relationships always have direction.
-
Relationships always have a type.
-
Relationships form patterns of data.