Neo4j -Cypher基本操作 (一)查询

提到Graph Database,目前使用的人还不够多。经过一个周的上手,感觉Graph DB可以解决传统Relational DB很多的局限。在所有NoSQL Database中,目前发展速度最快。可今天写一篇关于Neo4j基本操作的一些整理。

Query – 获取

  • Match Nodes and Relationships
    • Nodes
MATCH (n) RETURN n
MATCH (n) RETURN n LIMIT 50
MATCH (anyone :Person) RETURN anyone LIMIT 40
    • Relationships
MATCH (node1)-[rel]-(node2) 
RETURN node1, rel, node2

MATCH (actor:Actor)-[rel:ACTED_IN]->(movie:Movie)
RETURN actor, rel, movie
LIMIT 100

#returning the properties of nodes
MATCH (actor:Actor)-[rel:ACTED_IN|DIRECTED]->(movie:Movie)
RETURN actor.name, rel, movie.title
LIMIT 100
    • OPTIONAL MATCH
MATCH (movie:Movie)
MATCH (director:Person)-[:DIRECTOR]->(movie)
RETURN movie.title

#2.1
MATCH (movie:Movie)
MATCH (director:Person)-[:DIRECTED]->(movie)
MATCH (director)-[:ACTED_IN]->(movie)
RETURN movie.title, director.name

#2.2
MATCH (movie:Movie)
MATCH (director:Person)-[:DIRECTED]->(movie)<-[:ACTED_IN]-(director)
RETURN movie.title, director.name

#3
MATCH (movie:Movie)
OPTIONAL MATCH (director:Person)-[:DIRECTOR]->(movie)<-[:ACTED_IN]-(director)
RETURN movie.title, director.name

#2.1,#2.2产生的效果是一样的,都是返回仅为自导自演电影的名字及其导演姓名

#3 是用了OPTIONAL MATCH,可以返回所有的电影,如果director没有在电影中参演,就用null填充

    • 1 – If A has contact B, B has contact C, return A, B, C’s name
MATCH(a:Person)-[:HAS_CONTACT]->(b:Person)-[:HAS_CONTACT]->(c:Person)
RETURN a,b,c
LIMIT 1

MATCH(a:Person)-[:HAS_CONTACT]->(b:Person)-[:HAS_CONTACT]->(c:Person)
WHERE a<>c
RETURN a,b,c
LIMIT 1
    • 2 – list a contact’s name and along with a movie they directed if they directed one
MATCH (:Person)-[:HAS_CONTACT]->(p:Person)
OPTIONAL MATCH (p)-[:DIRECTED]->(movie:Movie)
RETURN p.name, movie.title
  • Filtering
    • MATCH + Nodes’ Property
MATCH (tom:Person{name:"Tom Hanks",born:1956})
RETURN tom
    • WHERE + Nodes’ Property + Operators
MATCH (tom:Person)
where tom.name = "Tom Hanks" AND tom.born = 1956
RETURN tom

#Comparison Operators
...
#Boolean Operators
MATCH (someone:Person)
where someone.born in [1956, 1957, 1958]
RETURN tom.name
    • PATH + Operators
MATCH (person:Person)--(movie:Movie)
WHERE movie.title = "Unforgiven" AND NOT (person)-[:DIRECTED]->(movie)
RETURN movie.title, person.name

#person and movie object can not be defined in WHERE CLAUSE, 
#Below won't work
MATCH (movie:Movie)
WHERE movie.title = "Unforgiven" AND NOT (person)-[:DIRECTED]->(movie)
RETURN movie.title, person.name
    • Regular Expression
MATCH (movie:Movie)
WHERE movie.title =~ '(?i).+The .*'
RETURN movie.title
    • Transform Result – LIMIT, SKIP, ORDER BY, AS
MATCH(person:Person)-[actor:ACTED_IN]->(movie:Movie)
WHERE movie.title = "Top Gun"
RETURN actor.name, role.earnings
ORDER BY role.earnings DESC
LIMIT 3
SKIP 10
  • Basic Functions
    • Remove Duplicated – DISTINCT
MATCH(person:Person)-[role:ACTED_IN]->(:Movie)
WHERE role.earnings > 10000000
RETURN DISTINCT person.name
    • Basic Statistics – COUNT, AVG, SUM, MIN, MAX
MATCH (tom:Person{name:"Tom Hanks",born:1956})-[:ACTED_IN]->(movie:Movie)
RETURN COUNT(movie) AS MovieCounts
    • String Function – check more on the development manual
RETURN toString(11.5), toString("already a string"), toString(TRUE)

RETURN replace("Hello World!", "l", "r")

RETURN replace(upper("oh my god"), "O", "Er")
    • Math Function – check more on the development manual
# haversin function as an example

RETURN floor(0.9), floor(2.1) // 0, 2

RETURN ceil(2.1), ceil(0.5) // 3, 1

RETURN round(0.9) // 1
  • 比较运算符 – Comparison Operators

<, >, <>, >=, <=

  • 逻辑运算符 – Boolean Operators

AND, NOT, OR, IN

  • Transform Result Clauses
    原文作者:Jack C
    原文地址: https://zhuanlan.zhihu.com/p/55363774
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞