Neo4j教程

Stella981
• 阅读 678

Cypher语句描述了如何使用Cypher语法操作图中的节点、关系和路径等元素,以下列举了一些常用的操作语句。

1. 查询操作Match语句

1.1 查询所有节点

MATCH(n) RETURN n

1.2 查询所有带标签的节点

MATCH(carSeries:CarSeries) 
RETURN carSeries.name

返回内容

carSeries.name
"宝马1系"
"宝马2系"
"宝马3系"

1.3 查询所有的节点并返回关联的节点

查询宝马5系下面有哪些车型

MATCH (carSeries {name: '宝马5系'})-[]-(carTye) 
RETURN carTye.carTypeName

返回结果

carTye.carTypeName
"2020款525L钻石款"
"2020款525L豪华款"
"2020款525L运动款"

1.4 查询节点关联的标签

MATCH (:CarSeries {name: '宝马5系'})-[]-(carTye:CarType) 
RETURN carTye.carTypeName

1.5 查询节点带指向关系

MATCH (:CarSeries {name: '宝马5系'})-[]->(carTye)
RETURN carTye.carTypeName

1.6 查询返回关系

MATCH (:CarSeries {name: '宝马5系'})-[r]->(carTye)
RETURN type(r)

返回内容

type(r)
"include"
"include"
"include"

1.7 根据关系类型查询

查询供应M34型号底盘的供应商

MATCH (:CarChassis {chassisName: 'M34'})<-[:supplier]-(supplier) 
RETURN supplier.supplierName

supplier.supplierName
"摩比斯"
"摩比斯"

1.8 查询多层关系

MATCH p = (carBrand{brandName:'宝马'}) - [:include*2] - (carType) 
RETURN relationships(p)

返回结果

elationships(p)
[
{
  "identity": 8,
  "start": 16,
  "end": 20,
  "type": "include",
  "properties": {

  }
}
,
{
  "identity": 17,
  "start": 20,
  "end": 29,
  "type": "include",
  "properties": {

  }
}
]
[
{
  "identity": 8,
  "start": 16,
  "end": 20,
  "type": "include",
  "properties": {

  }
}
,
{
  "identity": 18,
  "start": 20,
  "end": 30,
  "type": "include",
  "properties": {

  }
}
]
[
{
  "identity": 8,
  "start": 16,
  "end": 20,
  "type": "include",
  "properties": {

  }
}
,
{
  "identity": 19,
  "start": 20,
  "end": 31,
  "type": "include",
  "properties": {

  }
}
]
[
{
  "identity": 8,
  "start": 16,
  "end": 20,
  "type": "include",
  "properties": {

  }
}
,
{
  "identity": 20,
  "start": 20,
  "end": 32,
  "type": "include",
  "properties": {

  }
}
]
[
{
  "identity": 10,
  "start": 16,
  "end": 22,
  "type": "include",
  "properties": {

  }
}
,
{
  "identity": 21,
  "start": 22,
  "end": 33,
  "type": "include",
  "properties": {

  }
}
]
[
{
  "identity": 10,
  "start": 16,
  "end": 22,
  "type": "include",
  "properties": {

  }
}
,
{
  "identity": 22,
  "start": 22,
  "end": 34,
  "type": "include",
  "properties": {

  }
}
]
[
{
  "identity": 10,
  "start": 16,
  "end": 22,
  "type": "include",
  "properties": {

  }
}
,
{
  "identity": 23,
  "start": 22,
  "end": 35,
  "type": "include",
  "properties": {

  }
}
]

1.9 根据节点Id或关系ID查询

MATCH(n) 
WHERE id(n)=22 
RETURN n

根据关系ID查询 

MATCH()-[r]->() 
where id(r)=8 
RETURN r

返回关系结果

r
{
  "identity": 8,
  "start": 16,
  "end": 20,
  "type": "include",
  "properties": {

  }
}

2. 可选查询OPTIONAL MATCH语句

OPTIONAL MATCH与MATCH语句类型,不同的是,如果查询没有匹配到结果的话就会返回 null 。在这点上,OPTIONAL MATCH有些像SQL中的 out join 语句。

OPTIONAL MATCH的表现方式与WHERE语句类似,具体体现效果请看下面内容。

2.1 查询宝马 2020款320L运动款 下面关联的节点

MATCH(a:CarType{carTypeName:'2020款320L运动款'}) 
OPTIONAL MATCH(a)-[]->(x) 
RETURN x

Neo4j教程

3. 返回RETURN语句

3.1 返回所有的对象

MATCH p=(a {brandName:'宝马'}) -[r] - (b) 
RETURN *

返回结果

Neo4j教程

3.2 返回多个表达式

MATCH (a {brandName:'宝马'}) 
RETURN a.brandName, "牛逼Class", (a)-[]-()

返回结果

a.brandName

"牛逼Class"

(a)-[]-()

"宝马"

"牛逼Class"

[

{ "start": { "identity": 16, "labels": [ "CarBrand" ], "properties": { "brandName": "宝马", "createDate": "1916-03-07" } }, "end": { "identity": 28, "labels": [ "CarSeries" ], "properties": { "name": "宝马Z系", "description": "宝马入门级跑车", "status": "INUSE" } }, "segments": [ { "start": { "identity": 16, "labels": [ "CarBrand" ], "properties": { "brandName": "宝马", "createDate": "1916-03-07" } }, "relationship": { "identity": 16, "start": 16, "end": 28, "type": "include", "properties": {

    }
  },
  "end": {

"identity": 28, "labels": [ "CarSeries" ], "properties": { "name": "宝马Z系", "description": "宝马入门级跑车", "status": "INUSE" } } } ], "length": 1.0 }

,

{ "start": { "identity": 16, "labels": [ "CarBrand" ], "properties": { "brandName": "宝马", "createDate": "1916-03-07" } }, "end": { "identity": 27, "labels": [ "CarSeries" ], "properties": { "name": "宝马X系", "description": "宝马SUV车", "status": "INUSE" } }, "segments": [ { "start": { "identity": 16, "labels": [ "CarBrand" ], "properties": { "brandName": "宝马", "createDate": "1916-03-07" } }, "relationship": { "identity": 15, "start": 16, "end": 27, "type": "include", "properties": {

    }
  },
  "end": {

"identity": 27, "labels": [ "CarSeries" ], "properties": { "name": "宝马X系", "description": "宝马SUV车", "status": "INUSE" } } } ], "length": 1.0 }

]

 

 

 

4. WITH 语句

with从句可以连接多个查询的结果,将上一个查询的结果用作下一个查询的开始。

WITH语句的两种常用方法:

  • 通过使用oder by 和limit,with可以限制传入下一个match子查询语句的实体数目。
  • 对聚合值过滤。

4.1 对聚合结果进行过滤

MATCH(carBrand{brandName: '宝马'})-[]-(unkonwnNode)-[]->()
WITH unkonwnNode, count(*) as cn 
WHERE cn>1 
RETURN unkonwnNode.id

4.2 Order 排序

MATCH(n) WITH n 
ORDER BY n.id DESC 
LIMIT 3 
RETURN n

4.3 路径搜索限制分支数

MATCH(n{name:'宝马3系'})-[]-(m) 
WITH m 
ORDER BY m.name DESC 
LIMIT 1 
MATCH (m)-[]-(o) 
RETURN o.name

5. WHERE语句

MATCH(n)
WHERE n.name='Peter' XOR (n.age<30 AND n.name='Fucky') OR NOT (n.name='Peter')
RETURN n.name, n.age

6. ORDER BY语句

MATCH(n)
RETRUN n.length, n.name 
ORDER BY n.length

7. SKIP和LIMIT语句

MATCH(n) 
RETURN n.name
ORDER BY n.name
SKIP 3
LIMIT 2

8. DELETE 语句

DELETE语句用来删除图中的节点、关系或路径

MATCH(n:Useless)
DELETE n

9. REMOVE 语句

REMOVE语句用来移除节点的属性或者标签

MATCH(address {name: 'zhangsan'})
REMOVE address.age
RETURN address
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这