Neo4j使用简单例子
Neo4j Versions
Most of the examples on this page are written with Neo4j 2.0 in mind, so they skip the START
clause, and use clauses like MERGE
. The focus of this page is about Cypher-to-C# syntax though, and should be equally useful in helping you translate a Neo4j 1.9 query to C#.
At the end of the day, you always need to start with a working Cypher query, then work out the equivalent C#.
Need more help?
If you have a working Cypher query, but can't translate it to C#, just post it on http://stackoverflow.com/questions/tagged/neo4jclient and we'll help you out pretty quickly.
Then, once we have the answer, we can add it to this page too so it helps other people.
User class
Most of the examples below assume you have the following class, to represent the structure of a user node:
public class User
{
public long Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
Get all users by label
This Cypher:
MATCH (user:User)
RETURN user
Is this C#:
graphClient.Cypher
.Match("(user:User)")
.Return(user => user.As<User>())
.Results
Get specific user
This Cypher:
MATCH (user:User)
WHERE user.Id = 1234
RETURN user
Is this C#:
graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == 1234)
.Return(user => user.As<User>())
.Results
Get a user, and the count of their friends
This Cypher:
OPTIONAL MATCH (user:User)-[FRIENDS_WITH]-(friend:User)
WHERE user.Id = 1234
RETURN user, count(friend) AS NumberOfFriends
Is this C#:
graphClient.Cypher
.OptionalMatch("(user:User)-[FRIENDS_WITH]-(friend:User)")
.Where((User user) => user.Id == 1234)
.Return((user, friend) => new {
User = user.As<User>(),
NumberOfFriends = friend.Count()
})
.Results
Get a user, and all their friends
This Cypher:
OPTIONAL MATCH (user:User)-[FRIENDS_WITH]-(friend:User)
WHERE user.Id = 1234
RETURN user, collect(friend) AS NumberOfFriends
Is this C#:
graphClient.Cypher
.OptionalMatch("(user:User)-[FRIENDS_WITH]-(friend:User)")
.Where((User user) => user.Id == 1234)
.Return((user, friend) => new {
User = user.As<User>(),
Friends = friend.CollectAs<User>()
})
.Results
Create a user
This Cypher:
CREATE (user:User { Id: 456, Name: 'Jim' })
Should use parameters:
CREATE (user:User {newUser})
And is this C#:
var newUser = new User { Id = 456, Name = "Jim" };
graphClient.Cypher
.Create("(user:User {newUser})")
.WithParam("newUser", newUser)
.ExecuteWithoutResults();
Note that we're using an explicitly named parameter (newUser
) and the query, and the WithParam
method to supply it. This keeps our encoding safe, protects against Cypher-injection attacks, and improves performance by allowing query plans to be cached.
Create a user, only if they don't already exist
This Cypher:
MERGE (user:User { Id: 456 })
ON CREATE user
SET user.Name = 'Jim'
Should use parameters:
CREATE (user:User { Id: {id} })
ON CREATE user
SET user = {newUser}
And is this C#:
var newUser = new User { Id = 456, Name = "Jim" };
graphClient.Cypher
.Merge("(user:User { Id: {id} })")
.OnCreate()
.Set("user = {newUser}")
.WithParams(new {
id = newUser.Id,
newUser
})
.ExecuteWithoutResults();
Create a user and relate them to an existing one
This Cypher:
MATCH (invitee:User)
WHERE invitee.Id = 123
CREATE invitee-[:INVITED]->(invited:User {newUser})
Is this C#:
var newUser = new User { Id = 456, Name = "Jim" };
graphClient.Cypher
.Match("(invitee:User)")
.Where((User invitee) => invitee.Id == 123)
.Create("invitee-[:INVITED]->(invited:User {newUser})")
.WithParam("newUser", newUser)
.ExecuteWithoutResults();
Relate two existing users
This Cypher:
MATCH (user1:User), (user2:User)
WHERE user1.Id = 123, user2.Id = 456
CREATE user1-[:FRIENDS_WITH]->user2
Is this C#:
graphClient.Cypher
.Match("(user1:User)", "(user2:User)")
.Where((User user1) => user1.Id == 123)
.AndWhere((User user2) => user2.Id == 456)
.Create("user1-[:FRIENDS_WITH]->user2")
.ExecuteWithoutResults();
Relate two existing users, only if they aren't related already
This Cypher:
MATCH (user1:User), (user2:User)
WHERE user1.Id = 123, user2.Id = 456
CREATE UNIQUE user1-[:FRIENDS_WITH]->user2
Is this C#:
graphClient.Cypher
.Match("(user1:User)", "(user2:User)")
.Where((User user1) => user1.Id == 123)
.AndWhere((User user2) => user2.Id == 456)
.CreateUnique("user1-[:FRIENDS_WITH]->user2")
.ExecuteWithoutResults();
Update a single property on a user
This Cypher:
MATCH (user:User)
WHERE user.Id = 123
SET user.Age = 25
Is this C#:
graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == 123)
.Set("user.Age = {age}")
.WithParam("age", 25)
.ExecuteWithoutResults();
Note: we're using parameters again to pass in data. Never do this via string concatenation like Set("user.Age = " + age.ToString())
otherwise you will introduce encoding bugs, security risks, and significantly impact your query performance by bypassing the query plan cache in Neo4j itself.
Replace all the properties on a user
This Cypher:
MATCH (user:User)
WHERE user.Id = 123
SET user = { Id: 123, Age: 25, Email: 'tatham@oddie.com.au' }
Is this C#:
graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == 123)
.Set("user = {tatham}")
.WithParam("tatham", new User { Id = 123, Age = 25, Email = "tatham@oddie.com.au" })
.ExecuteWithoutResults();
Delete a user
This Cypher:
MATCH (user:User)
WHERE user.Id = 123
DELETE user
Is this C#:
graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == 123)
.Delete("user")
.ExecuteWithoutResults();
Delete a user and all inbound relationships
This Cypher:
OPTIONAL MATCH (user:User)<-[r]-()
WHERE user.Id = 123
DELETE r, user
Is this C#:
graphClient.Cypher
.OptionalMatch("(user:User)<-[r]-()")
.Where((User user) => user.Id == 123)
.Delete("r, user")
.ExecuteWithoutResults();
Get all labels for a specific user
This Cypher:
MATCH (user:User)
WHERE user.Id = 1234
RETURN labels(user)
Is this C#:
graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == 1234)
.Return(user => user.Labels())
.Results
Get all labels for a specific user, and still the user too
This Cypher:
MATCH (user:User)
WHERE user.Id = 1234
RETURN user, labels(user)
Is this C#:
graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == 1234)
.Return(user => new {
User = user.As<User>(),
Labels = user.Labels()
})
.Results
Get a user, count their friends then add this number to the user and return.
Note: This is an example of using Neo4j 3.0 Stored Procedures. There are other ways of adding a property to an object, this is just an example of CALL and YIELD, using apoc Neo4j Stored Procedures
This Cypher:
MATCH (user:User)
WHERE user.Id = 1234
WITH user, size((user)-[:IS_FRIENDS_WITH]->(:Friend)) as numberOfFriends
CALL apoc.map.setKey(user, 'numberOfFriends', numberOfFriends) YIELD value AS userWithFriends
RETURN userWithFriends
Is this C#:
graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == 1234)
.With("user, size((user)-[:IS_FRIENDS_WITH]->(:Friend)) as numberOfFriends")
.Call("apoc.map.setKey(user, 'numberOfFriends', numberOfFriends)").Yield("value AS userWithFriends")
.Return(userWithFriends => new {
User = userWithFriends.As<User>()
})
.Results
原文地址:https://www.cnblogs.com/ulex/p/6066083.html
Neo4j使用简单例子的更多相关文章
- Neo4j使用简单例子(转)
Neo4j Versions Most of the examples on this page are written with Neo4j 2.0 in mind, so they skip th ...
- Hibernate4.2.4入门(一)——环境搭建和简单例子
一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- ko 简单例子
Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...
- mysql定时任务简单例子
mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9 如果要每30秒执行以下语句: [sql] update userinfo set endtime = now() WHE ...
- java socket编程开发简单例子 与 nio非阻塞通道
基本socket编程 1.以下只是简单例子,没有用多线程处理,只能一发一收(由于scan.nextLine()线程会进入等待状态),使用时可以根据具体项目功能进行优化处理 2.以下代码使用了1.8新特 ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...
- [转] 3个学习Socket编程的简单例子:TCP Server/Client, Select
以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计 ...
随机推荐
- C++中调用OC代码
前言 最近项目中为了方便维护,底层统一使用C++编写.由于是项目是做屏幕共享sdk,所以只能底层的压缩.编解码使用C++,屏幕捕获部分Mac和win就自己实现了.那么问题就来了,因为是面向接口编程,所 ...
- (8)centos7 登录与关机
关机重启 shutdown -h now #立刻关机 shutdown -h 1 #一分钟后关机 shutdown -h 17:00 #指定时间关机(如果当前时间超过17:00,则会转到明天的17:0 ...
- Rollei SL66 使用说明
根据记忆,并用不规范的语言描述我对sl66的使用心得:一.上卷1.用摇把顺时针转到12点位置,再退回3点位置:2.安插刀:3.后背上方按钮向右拨,打开后背:4.取出,装卷,再放入:5.转动后背上旋钮, ...
- es-字段类型整理(6.x及以上)
以下为主要的数据类型,特殊的或者比较冷门的不予关注: 类型分类 子分类 具体类型 核心类型 字符串 text,keyword 整数 byte,short,integer,long 浮点 double, ...
- 基于OpenResty与Consul实现服务网格ServiceMesh
一.逻辑架构 1.基于OpenResty开发智能代理: 利用其动态可编程特性,动态化配置nginx服务路由: 2.需要向OpenResty添加weibo开源的upsync服务发现模块: 3.基于con ...
- 从零开始搭建系统1.3——Tomcat安装及配置
首先安装jdk,手动解压JDK的压缩包,然后设置环境变量 1.卸载自带openjdk 查询OpenJDK rpm -qa|grep java 批量卸载所有名字包含jdk的已安装程序.命令行: rpm ...
- 关于虚拟机中linux系统时间的问题
由于考试需求,我在vm上放置了考试用的linux环境,在进行操作的时候需要回调时间才能进行一些操作.但是每次重启之后,时间总是会和物理服务器的时间进行同步,让我非常的苦恼. 终于有一天我想清楚了如何表 ...
- IDEA2017.3.1破解激活
idea激活有多种方式,网上较多的是使用注册码或者填License server网址,目前(2017年8月19日)使用注册码的方式,亲测可用的只有lanyun提供的注册码,但是会在2017年11月份的 ...
- linux hg 查看当前提交的所有版本及注释
linux hg 查看当前提交的所有版本及注释? linux hg 查看当前提交的所有版本及注释? linux hg 查看当前提交的所有版本及注释? 回答: hg log
- K8S命令的梳理
kubectl是一个基础的K8S集群管理命令,可以实现对K8S资源的查询,创建,删除,更新,回退等各种各样的操作.由于其复杂的功能体系,命令灵活度又高,因此需要进行常见的一些命令和使用场景的梳理. 1 ...