NEO4J数据库查看和切换:
查看当前的数据库列表:
show databases
切换数据库:
use mydatabase
增删改:
唯一索引创建:
create constraint on (s:Student) assert s.name is unique
唯一索引删除:
drop constraint on (s:Student) assert s.name is unique
普通索引创建:
create index on: Student(name)
普通索引删除:
drop index on: Student(name)
边创建
match (a:Student),(b:Teacher) where a.name='dd' and b.name='tt' merge (a)-[r:teacher]->(b)
边删除
match merge (a)-[r:teacher]->(b) delete r
实体创建:
CREATE(n:TEST {name:'TEST-NAME', age:1})
实体删除(如果有边关系,不进行删除):
MATCH(n:City) delete n
实体删除(同步删除相应的边关系):
MATCH(n:City) detach delete n
实体创建或更新实体内容:
MERGE (c: Student {name:'xiaoli'})
ON MATCH SET c.age=12
on create set c.age=12
实体创建或更新:
match (f: Student), (c: Teacher)
where f.inner_code ={0} and c.inner_code = {0}
merge (c)-[r:teach]->(f)
批量操作载入实体:
LOAD CSV WITH HEADERS FROM "file:///企业.csv" AS line
MERGE (p:Enterprise{id:line.id,name:line.name})
批量操作载入边关系:
LOAD CSV WITH HEADERS FROM "file:///rela-11.csv" AS line
match (from:Enterprise{id:line.from_id}),(to:Enterprise{id:line.to_id})
merge (from)-[r:rel{pro1:line.pro1}]->(to)
查:
1、查询数组属性
match (n:Student) where "三生三世十里桃花" in n.readBooks return n
2、类似es的matchphrase查询
match (n:Student) where n.name contains '小' return n
3、精确查询
match (n:Student) where n.name='小白' return n
4、范围查询
match (n:Student) where n.age<20 return n
5、单级变关系查询
match (n:Student)-[r:teach]->(t:Teacher) where n.name='小白' return t
6、使用with关键字多级关系查询
match (n:Student)-[r:teach]->(t:Teacher) with t match (t:Teacher)-[:releasebook]->(b:Book) return t,b
7、使用深度运算符对于的变关系深度查询是[a,b]包含a和b
match (n:Student)-[*1..5]->(b:Book) where n.name='小白' return b
unwind是cypher提供的一种列表遍历工具,类似于python中的for循环,unwind结合case等语法可以写出许多复杂的查询,尤其是对于路径查询的处理。foreach也是cypher提供的一个列表遍历工具,但是主要用来做增删改。对于这两个的用法,建议结合列表推导一起学习
————————————————
版权声明:本文为优快云博主「大白兔黑又黑」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:,使用样例如下:
WITH [[1, 2],[3, 4], 5] AS nested
UNWIND nested AS x
UNWIND x AS y
RETURN y
oreach是cypher提供的列表更新工具,官方文档说明:The FOREACH clause is used to update data within a list, whether components of a path, or result of aggregation. 注意两点,update和list。在foreach中,可以执行CREATE, CREATE UNIQUE, MERGE, DELETE,等更新语法,还可做foreach嵌套。
MATCH p =(begin)-[*]->(END )
WHERE begin.name = 'A' AND END .name = 'D'
FOREACH (n IN nodes(p)| SET n.marked = TRUE )
参考:
neo4j官网文档:Neo4j Cypher Refcard 4.4
https://blog.youkuaiyun.com/haveanybody/article/details/89634778