Cypher Syntax Encyclopedia
Neo4j example
After installing neo4j, visit the corresponding web page port and enter the following command to learn
node operation
create node
- Create a node labeled stu (label: classify data for easy management)
create(n:stu);// 这里的n就是随便起的一个变量名,代指当前实体或者关系,下文会使用到。
- Create a node with the label stu and the attribute value
create(n:stu{
id:1,name:'小明',age:20});
query node
- Search by label
match (n:stu) return n;
// 上文说到 n类似变量名, 一般可用于后续操作,
// match n:stu 指查询到标签为stu的节点 赋值给变量n ,return n则是将这批节点返回;
The running result is as shown in the figure below.
Neo4j uses the name attribute for display by default.
- Query by id
match (n:stu) where id(n) = 38023 return n;
//此id是neo4j默认分配给实体的id 不是上面定义的属性“id”
- Query by attribute
match (n:stu) where n.name = '小明' return n;
- Multi-attribute conditional query
match (n:stu) where n.name = '小明'and n.age = 20 and n.id = 1 return n;
edit entity
- Add multi-label
// neo4j 支持多标签
match (n:stu) where id(n) = 38023 set n:stu1 return n;
match (n:stu) where id(n) = 38023 set n:stu2 return n;
You can see that the node has three labels
- remove label
match (n:stu) where id(n) = 38023 remove n:stu2 return n;
match (n:stu) where id(n) = 38023 remove n:stu1 return n;
- modify a property
match (n:stu) where n.name='小明' set n.name = '小红' return n;
- Modify multiple attribute values
match (n:stu) where n.name='小红' set n.age = '200' set n.id = 2 return n;
You can see that the right side has been modified successfully
delete entity
//创建测试数据
create(n:stu{
id:1,name:'小王',age:20});
//查询出来并删除
match (n:stu) where n.name = '小王' delete n;
If the deletion is successful, you can see a return prompt that an entity has been deleted
relational operations
Create new relationships and entities
This operation does not require an entity to be created in advance
//创建标签为stu 属性name 的两个实体、标签为test的关系一个
create (n:stu{
name:'小张'})-[r:test] -> (m:stu{
name:'小李'})
//关系和实体同理 关系新建时也可以添加属性
create (n:stu{
name:'小黄'})-[r:test{
name:'夫妻'}] -> (m:stu{
name:'小八'})
Note: Use match (n:stu) return n This command queries the entities and relationships of the stu tag
Existing Entity New Relationship
This operation requires the entity to be created in advance
// 这两行是一条命令 分割一下看的清晰 第一步就是查出来 m、n实体 第二布就是给他俩创建关系
match (n:stu),(m:stu) where n.name = '小李' and m.name = '小八'
create (n)-[r:test{
name:'儿子'}]->(m) ;
At this time, we can see that these four entities have been connected by matching
edit relationship
- modify properties
// 修改关系属性
match p = (n)-[r:test]->(m) where r.name='夫妻' set r.name='朋友';
// 我们想修改完后直接可以看到修改结果 我们在后面加一个return即可 将n-r-m赋值给一个变量 然后return这个变量 如下
match p = (n)-[r:test]->(m) where r.name='朋友' set r.name='闺蜜' return p;
//可以看到下放返回的不是提示信息 而是实体和关系 如下图
- relationship add property
// 直接set 新属性赋值即可
match p = (n)-[r:test]->(m) where r.name='闺蜜' set r.color='红色' return p;
- Modify the label
Note that neo4j does not support modifying the relationship label , so our operation of modifying the label is to recreate a relationship and delete the original relationship. This operation has no effect on use. It should be noted that the id of the relationship will change
// 四行是一条命令 第一行 搜出来想要修改的关系 第二行新建关系 第三行复制原属性 第四行删除原关系
MATCH (n)-[r:test]->(m) where r.name='闺蜜'
CREATE (n)-[r2:relation]->(m)
SET r2 = r
DELETE r
delete relationship
//直接match 锁定到某个或某几个关系 delete即可
match (n)-[r:relation]->(m) where r.name = '闺蜜' delete r;
//不确定的话 可以将delete换成return 先看一下返回的是不是要删的 然后再进行删除 命令如下
match p = (n)-[r:relation]->(m) where r.name = '闺蜜' return p;
Advanced Search
contains
//包含
match (n:stu) where n.name Contains '小' return n
starts with
//开始于某条件 类似 like 小%
match (n:stu) where n.name starts with '小' return n
ends with
//结束于某条件 类似 like %红
match (n:stu) where n.name ends with '红' return n
limit
//限制返回条数 放在最后使用 return 后面
match (n:stu) return n limit 2;
SKIP
// 跳过几条 然后返回
match (n:stu) return n skip 3;
paging
//limit 和skip组合使用就是分页
match (n:stu) return n skip 2 limit 2;
order by
- ascending order
//默认是升序 也可以加上 asc 效果一样
// 注意 id(n) 这个写法只针对于 默认生成的id 如果是你添加的属性不可以这么写
match(n:stu) return n order by id(n) ;
match(n:stu) return n order by id(n) asc;
//自己添加的属性要用 n.name
match(n:stu) return n order by n.id asc;
- descending order
match(n:stu) return n order by n.id desc;