Cypher - [OPTION] MATCH

本文深入探讨了Cypher查询语言的各种高级用法,包括条件表达式、节点和关系的匹配、属性过滤、字符串匹配、返回类型及标签类型、可变长关系查询、指定用户关系查询、最短路径查找等,为Neo4j数据库的高效操作提供了全面指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

条件表达式

CASE test
WHEN value THEN result
[WHEN ...]
[ELSE default]
END

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

 match(n:User)
where n.id = 1
return case n.id when 1 then '管理员' else '普通人员' end as result

match(n:User)
where n.id = 1
return case  when n.id=1 then '管理员' else '普通人员' end as result

// 查询所有节点

match (n)
return n

//节点和属性过滤

match (me)
where me.id = 1 and me:User
return me

//检查【节点】【属性】的存在性

match (me)
where exists(me.age)
return me

//字符串匹配(区分大小写)

match (u)
where u.name starts with 'a' and u.name ends with 'z' or u.name contains 'zs' 
return u

// 匹配多种关系中的一种 , 使用  |

match (n) -[r :ORG | :DEP]-> ()
return n

// 返回类型类型

match (n) -[r]- ()
return type(r)

//返回标签类型

match (n:User)
where n.id = 1
return labels(n)

//返回多个类型 【我朋友的朋友】

match (me:User) -->(friend:User) --> (friend_of_friend:User)
return  friend_of_friend

可变长关系   -[:TYPE * minHops .. maxHops]->

// 返回 1 至 2 跳的以内的用户

match (me:User{id:1})  -[*1..2]->(n:User)
return n

// 返回 0跳用户,就是返回自己

match (me:User{id:1})  -[*0]->(n:User)
return n

//返回至少有 2跳的用户

match (me:User)  -[*2..]-> (n:User)        // -[*2..]-> 等于 -[*2]->
return me     

//返回小于 2跳的用户, 注意不包含 0跳用户, 如果要包含应该是  -[*0..2]->

match (me:User)  -[*..2]->(n:User)            // -[*..2]-> 等于 -[*1..2]->
return me       

// 返回数据库中节点的id。 注意neo4j 会重用已经删除的节点的id

match (me:User)
where me.id = 3
return id(me)

返回指定用户的所有关系 

match (u1:User) -[*]-> (u2:User)
where u1.id = 1                     
// -[*]-> 表示跳数不限制
return *           

match (u1:User) --> (u2:User) where u1.id = 1 return *                     //只表示返回用户本身,及其1跳的用户

返回找到指定用户,返回跳数大于2的路径

match p=(u1:User)-[*2]->(u2:User)
where u1.id = 1
return p

// 指定两个节点,找出其最短路径

match (u1:User),(u2:User),sp = shortestPath((u1)-[*]->(u2))
where u1.id = 1 and u2.id = 3  
return sp

//带断言的最短路径

 

//所有最短路径

match (u1:User),(u2:User),p = allShortestPaths((u1) -[*]-> (u2) )
where u1.id = 1 and u2.id = 3  
return p

//模式过滤,注意 where 中的模式是过滤 match 之后的结果

match (u),(o)
where u.id in [1,2] and not (u)-->(o)
return *

optinal match

用于搜索模式中描述的匹配项,对于找不到的项目用null 代替

match (me:User)
where me.id = 32
optional match (me)-->(o) 
return o

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/2552286/blog/3070327

neo4j-driver 是 Neo4j 数据库的官方驱动,用于在编程语言中与 Neo4j 进行交互。ECharts 是一个使用 JavaScript 实现的开源可视化库,它能够在网页中展示数据的图表。 要在 ECharts 中展示 Neo4j 数据库中的数据,你需要结合使用 neo4j-driver ECharts。以下是大致步骤: 1. 使用 neo4j-driver 连接到 Neo4j 数据库并执行 Cypher 查询语句,获取需要展示的数据。 2. 将查询得到的数据转换成 ECharts 可以使用的格式,比如 JSON。 3. 在 ECharts 图表配置中使用转换后的数据,并按照指定的图表类型进行配置。 4. 在网页中嵌入 ECharts 图表,并将数据绑定到图表上,展示出来。 这里是一个简化的示例代码: ```javascript // 1. 连接到 Neo4j 数据库 const neo4j = require('neo4j-driver'); const driver = neo4j.driver('bolt://localhost:7687', neo4j.auth.basic('neo4j', 'password')); // 2. 定义一个函数来获取数据 function fetchData() { return new Promise((resolve, reject) => { const session = driver.session(); const query = 'MATCH (n) RETURN n LIMIT 10'; // 示例查询,获取前10个节点 session.run(query) .then(result => { const data = result.records.map(record => { return { name: record.get('n').properties.name, // 假设节点有一个名为name的属性 value: record.get('n').properties.value // 假设节点有一个名为value的属性 }; }); resolve(data); session.close(); }) .catch(error => { reject(error); }); }); } // 3. 获取数据并配置 ECharts fetchData().then(data => { const option = { title: { text: '节点分布图' }, tooltip: {}, legend: { data:['销量'] }, xAxis: { data: data.map(d => d.name) }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: data.map(d => d.value) }] }; // 4. 在网页中初始化 ECharts 图表并使用配置 const myChart = echarts.init(document.getElementById('main')); myChart.setOption(option); }); ``` 在上面的代码中,我们首先连接到 Neo4j 数据库,然后定义了一个 `fetchData` 函数来获取数据。之后,我们定义了 ECharts 的配置对象 `option`,并使用获取到的数据来填充这个对象。最后,我们使用 ECharts 的 `init` 函数初始化图表,并使用 `setOption` 方法将配置应用到图表上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值