Cypher syntax of Neo4j

1. Cypher type

The type system supported by Cypher is divided into three categories: attribute types, composite types, and struct types.

1.1 Attribute Type

Property types: Integer, Float, String, and Boolean

Boolean type: true, false, TRUE, FALSE

String type: 'Hello', "World"

1.2 Composite types

Composite types: List and Map, List is an ordered collection of values, and Map is an unordered collection of Key/Value pairs, usually used to store properties of nodes and relationships.

List type: ['a', 'b'], [1, 2, 3], ['a', 2, n.property, $param], [ ]

Map type: n is the node, prop is the property key of the node, the format of the value of the reference property: n.prop,

1.3 Structure Type

Structure type: Node type, relation type, Path type:

  • The Node type represents a node, which consists of Id, Label and Map;
  • The relationship type represents a relationship, which consists of Id, Type, Map and the Ids of the two nodes at both ends of the relationship;
  • The Path type represents a path, which is a sequence of nodes and relationships. Path mode: (a)-->()<--(b)

2. Naming rules and scope

2.1 Naming rules

The name starts with an English character and can contain numbers, but numbers cannot be used as the first character, and cannot contain other symbols except underscore and $. Note that underscores should be used in the middle or end of the name, for example, my_variable.

If a symbol can only be used at the beginning of a name, indicate a parameter, such as myParam.

Names are case sensitive, :PERSON and :Person are different.

2.2 Scope

Node's Lable, relationship type, and property key are in different scopes (Scopes). Under the same Scope, the names are not allowed to be repeated; however, in different scopes, the names are allowed to be repeated and have different meanings .

CREATE (a:a {a: 'a'})-[r:a]→(b:a {a: 'a'})

3. CASE expressions

The case expression processes the results of the query according to the conditions:

CASE
WHEN predicate THEN result
  [WHEN ...]
  [ELSE default]
END

4. Query parameters

The participating syntax is $Param, and the name of the parameter is Param. When passing a parameter, you must ensure that the passed parameter has the same name as the parameter in the Query.

Query with Cypher is, the parameter is $ids,

MATCH (n)
WHERE id(n) IN $ids
RETURN n.name

The parameters passed to Query are:

{
  "ids" : [ 0, 1, 2 ]
}

5. Operators

The operator is to perform arithmetic operations, logical operations, etc. on Cypher queries.

5.1 Common Operators

distinct is used to remove duplicate values, n.property is used to access properties, [] is a list of variables

CREATE (a:Person { name: 'Anne', eyeColor: 'blue' }),(b:Person { name: 'Bill', eyeColor: 'brown' }),(c:Person { name: 'Carol', eyeColor: 'blue' })
WITH [a, b, c] AS ps
UNWIND ps AS p
RETURN DISTINCT p.eyeColor

5.2 Mathematical operators

  • Addition, subtraction, multiplication and division: +,-,*,/
  • Modulo: %
  • exponentiation: ^

5.3 Comparison Operators

  • Equal to:=
  • Not equal to: <>
  • Less than, greater than, less than or equal to, greater than or equal to: <, >, <=, >=
  • IS NULL and IS NOT NULL

5.4 Logical Operators

AND (AND), OR (OR), XOR (XOR), NOT (NOT)

WITH [2, 4, 7, 9, 12] AS numberlist
UNWIND numberlist AS number
WITH number
WHERE number = 4 OR (number > 6 AND number < 10)
RETURN number

5.5 Strings

String concatenation: +

Matching regular: =~

WITH ['mouse', 'chair', 'door', 'house'] AS wordlist
UNWIND wordlist AS word
WITH word
WHERE word =~ '.*ous.*'
RETURN word

For strings, use STARTS WITH, ENDS WITH, and CONTAINS to filter strings:

WITH ['John', 'Mark', 'Jonathan', 'Bill'] AS somenames
UNWIND somenames AS names
WITH names AS candidate
WHERE candidate STARTS WITH 'Jo'
RETURN candidate

5.6 List Operations

  • +, list append
  • IN: check member
  • []: index, in particular, [start .. end], starting from start, incremented by 1, but not including end
RETURN [1,2,3,4,5]+[6,7] AS myList

WITH [2, 3, 4, 5] AS numberlist
UNWIND numberlist AS number
WITH number
WHERE number IN [2, 3, 8]
RETURN number

WITH ['Anne', 'John', 'Bill', 'Diane', 'Eve'] AS names
RETURN names[1..3] AS result

5.7 Accessing properties

Use . to access properties

MATCH (n) WHERE 21 < n.age AND n.age <= 30 RETURN n

Reference: https://www.cnblogs.com/ljhdo/p/10911426.html

Tags: neo4j

Posted by Aeolus on Tue, 20 Sep 2022 02:03:32 +0930