Neo4j Cypher语法整理

Stella981
• 阅读 582
  • 查询John的朋友的朋友

添加5个人名节点,名字分别为

CREATE (:People{name:"John"})
CREATE (:People{name:"Joe"}) 
CREATE (:People{name:"Sara"}) 
CREATE (:People{name:"Steve"}) 
CREATE (:People{name:"Maria"})

此时查询所有人的结果如下

match (people:People) return people

Neo4j Cypher语法整理

给这5个人添加上朋友关系

MATCH (a:People{name:"John"}) MATCH (b:People{name:"Joe"}) CREATE (a) -[:friend]->(b)
MATCH (a:People{name:"John"}) MATCH (b:People{name:"Sara"}) CREATE (a) -[:friend]->(b)
MATCH (a:People{name:"Joe"}) MATCH (b:People{name:"Steve"}) CREATE (a) -[:friend]->(b)
MATCH (a:People{name:"Sara"}) MATCH (b:People{name:"Maria"}) CREATE (a) -[:friend]->(b)

再次查询

match (people:People) return people

Neo4j Cypher语法整理

查询John朋友的朋友的名字

match (john {name:'John'})-[:friend]->()-[:friend]->(fof) return john.name,fof.name

结果

Neo4j Cypher语法整理

因为这里查询的只是名字,而不是节点,所以只会显示各自的名称。

  • 给定一个用户名列表,找到名字在列表中的所有节点。匹配他们的朋友,仅返回那些他们关注的name属性以'S'开头的用户

    match (user)-[:friend]->(follower) where user.name in ['Joe','John','Sara','Maria','Steve'] and follower.name =~'S.*' return user.name,follower.name

结果

Neo4j Cypher语法整理

  • 获取John和他的朋友数量

    match (n {name:'John'})-[:friend]->(friend) with n,count(friend) as friendsCount return n,friendsCount

Neo4j Cypher语法整理

  • 获取John的朋友数量并保存为John的一个属性

    match (n {name:'John'})-[:friend]->(friend) with n,count(friend) as friendsCount set n.friendsCount=friendsCount return n.friendsCount

Neo4j Cypher语法整理

  • 查询朋友的朋友,不包含自己

添加3个用户,两个好友关系

create (adam:User {name:'Adam'}),(pernilla:User {name:'Pernilla'}),
(david:User {name:'David'}),(adam)-[:friend]->(pernilla),
(pernilla)-[:friend]->(david)

查询结果

match (n:User) return n

Neo4j Cypher语法整理

match (user {name:'Adam'})-[:friend]->()-[:friend]->(friend) return friend

Neo4j Cypher语法整理

match (user {name:'Adam'})-[:friend]->(friend),(friend)-[:friend]-(alluser) return alluser

 该方式也是不包含自己的,可以与下面包含自己的做出比较。

  • 查询朋友的朋友,包含自己

    match (user {name:'Adam'})-[:friend]->(friend) match (friend)-[:friend]-(alluser) return alluser
    

    Neo4j Cypher语法整理

点赞
收藏
评论区
推荐文章
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
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
待兔 待兔
2个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Stella981 Stella981
2年前
List的Select 和Select().tolist()
List<PersondelpnewList<Person{newPerson{Id1,Name"小明1",Age11,Sign0},newPerson{Id2,Name"小明2",Age12,
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
2年前
JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak
Wesley13 Wesley13
2年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
ES6 新增的数组的方法
给定一个数组letlist\//wu:武力zhi:智力{id:1,name:'张飞',wu:97,zhi:10},{id:2,name:'诸葛亮',wu:55,zhi:99},{id:3,name:'赵云',wu:97,zhi:66},{id:4,na
Wesley13 Wesley13
2年前
mysql select将多个字段横向合拼到一个字段
表模式:CREATE TABLE tbl_user (  id int(11) NOT NULL AUTO_INCREMENT,  name varchar(255) DEFAULT NULL,  age int(11) DEFAULT NULL,  PRIMARY KEY (id)