Neo4j 常用命令 一

WHERE

可以类似于SQL的写法来实现,首先通过match将节点查询出来(n,m),然后通过WHERE将数据

match (n:Lianhuachi),(m:Lianhuachi) where n.name = '去种田的向凹凸' and m.name = "向云朵"
return id(n),n.name,id(m),m.name
match (n:Lianhuachi {name: "去种田的向凹凸"}),(m:Lianhuachi {name: "向云朵"})
return id(n),n.name,id(m),m.name

DELETE-删除节点

  • 删除节点(如果节点存在关系时,是无法删除的)
match (n: Person {name: "xxx"}) delete n
  • 删除关系
match (n: Person {name: "小红"})-[r]->(m: Person {name: "小明"}) delete r

REMOVE-删除属性

首先,在student和person标签创建1一个节点

create (n:student:person {name: "张三", age: 18}) return n

查询节点

MATCH (n:person) where n.name = "张三" RETURN n LIMIT 25

MATCH (n:student) RETURN n LIMIT 25

删除标签中的节点

MATCH (n:person) where n.name = "张三" remove n

删除标签中节点的属性

删除前,age=18 MATCH (n:student) where n.name = "张三" return n

删除后,age无了 MATCH (n:student) where n.name = "张三" remove n.age return n

修改标签中节点的属性

修改后,age回来了 MATCH (n:student) where n.name = "张三" set n.age=18 return n

给榜首增加一个属性 match (n:Lianhuachi {name: "我是野农"}) set n.title = '虾米榜榜首' return n

ORDER BY - 排序

补充测试数据
create (n:student {name: "张三", age: 18}),(m:student {name: "李四", age: 22}) return n,m

排序

match (n:student) return id(n), n.name,n.age order by n.age desc

id(n) n.name n.age
120 "李四" 22
119 "张三" 18

NULL - NULL属性

查询name属性不为空的节点
match (n:student) where n.name is not null return n.name,n.label

n.name n.label
"张三" null
"李四" null

查询sex属性为空的节点
match (n:student) where n.sex is null return n.name,n.label

n.name n.label
"张三" null
"李四" null

张三设置sex属性,注意where在前,set在后,和MySQL等相反
match (n:student) where n.name='张三' set n.sex='男' return n.name,n.sex,n.age
查询结果:match (n:student) return n

IN - 查询多个

查询name等于张三或李四的人:
match (n:student) where n.name in ["张三", "李四"] return n

索引

官方文档

旧版本

  • 创建索引:
    create index on :节点 (属性)

  • 删除索引:
    drop index on :节点(属性)

新版本(5或更高版本)

  • 创建索引:
    缺省索引名称:
    CREATE INDEX [index_name(可缺省)] FOR (n:person) ON (n.name)
    指定索引名称:
    create index index_test for (n:person) on (n.name)
    Added 1 index, completed after 992 ms.

  • 查询所有索引:
    show indexes

id name state populationPercent type entityType labelsOrTypes properties indexProvider owningConstraint lastRead readCount
1 "index_343aff4e" "ONLINE" 100 "LOOKUP" "NODE" null null "token-lookup-1.0" null "2024-05-18T13:26:33.878000000Z" 571
3 "index_a302cc54" "ONLINE" 100 "RANGE" "NODE" ["person"] ["name"] "range-1.0" null null 0
2 "index_f7700477" "ONLINE" 100 "LOOKUP" "RELATIONSHIP" null null "token-lookup-1.0" null "2024-05-10T00:48:00.395000000Z" 67
  • 删除索引:
    drop index index_a302cc54
    Removed 1 index, completed after 5 ms.