首页 文章

Neo4j Cypher查询删除节点和所有子节点和关系

提问于
浏览
-1

这是我在Cypher中的Neo4j代码的一部分:

CREATE 
(search{id : '0', title : 'Begin Search'}),
(telephone { id : '2' , title : 'Telephone' }),
(tablet { id : '1' , title : 'Tablet' }),
(printer { id : '3' , title : 'Printer' }),
(scanner { id : '4' , title : 'Scanner' }),
(laptop { id : '5' , title : 'Laptop' }),
(pc { id : '6' , title : 'Personal Computer' }),
(monitor { id : '7' , title : 'Monitor' }),

(galaxykids7wifi { id : '100' , model : ' Galaxy Tab 3 Kids 7.0 wi-fi', brand : 'Samsung', processor : 'Dual-Core Processor', network : 'Android Jelly Bean 4.1', memory : '8 Gb' , screen : '7 inches ', wieght : '302 g', dimensions : '111.1*188*9.9 mm' , battery : '4000 mAh', wifi : '802.11', wifi_speed : '2.4 + 5 GHz', wifi_type : 'a/b/g/n', ram : '1 Gb', kamera : '3 Mpix' }),
(galaxytab310wifi3g { id : '101' , model : ' Galaxy Tab 3 10.1 wi-fi + 3 G', brand : 'Samsung', processor : 'Dual-Core Intel Atom Processor', network : 'Android 2.2', memory : '16 Gb', memory1 : '32 Gb' , screen : '10.1 inches ', wieght : '512 g', dimensions : '176.1*243,1*7.45 mm' , battery : '6800 mAh', wifi : '802.11', wifi_speed : '2.4 + 5 GHz', wifi_type : 'a/b/g/n', ram : '1 Gb', kamera : '3 Mpix' }),
(galaxytab310wifi { id : '102' , model : ' Galaxy Tab 3 10.1 wi-fi', brand : 'Samsung', processor : 'Dual-Core Intel Atom Processor', network : 'Android 2.2', memory : '16 Gb',memory1 : '32 Gb' , screen : '10.1 inches ', wieght : '510 g', dimensions : '176.1*243,1*7.45 mm' , battery : '6800 mAh', wifi : '802.11', wifi_speed : '2.4 + 5 GHz', wifi_type : 'a/b/g/n', ram : '1 Gb', kamera : '3 Mpix' }), 
(galaxytab38wifi3g { id : '103' , model : ' Galaxy Tab 3 8.0 wi-fi + 3 G', brand : 'Samsung', processor : 'Dual-Core Processor', network : 'Android JB 4.2.2', memory : '16 Gb', memory1 : '32 Gb' , screen : '8 inches ', wieght : '316 g', dimensions : '123.8*209.8*7.4 mm' , battery : '4450 mAh', wifi : '802.11', wifi_speed : '2.4 + 5 GHz', wifi_type : 'a/b/g/n', ram : '1.5 Gb', kamera : '5 Mpix'}),
(galaxytab38wifi { id : '104' , model : ' Galaxy Tab 3 8.0 wi-fi', brand : 'Samsung', processor : 'Dual-Core Processor', network : 'Android JB 4.2.2', memory : '16 Gb', memory1 : '32 Gb' , screen : '8 inches ', wieght : '314 g', dimensions : '204.8*123.8*7.4 mm' , battery : '4450 mAh', wifi : '802.11', wifi_speed : '2.4 + 5 GHz', wifi_type : 'a/b/g/n', ram : '1.5 Gb', kamera : '5 Mpix'}),

(search)<-[:TYPE]-(tablet),
(search)<-[:TYPE]-(telephone),
(search)<-[:TYPE]-(printer),
(search)<-[:TYPE]-(scanner),
(search)<-[:TYPE]-(laptop),
(search)<-[:TYPE]-(pc),
(search)<-[:TYPE]-(monitor),

(tablet)<-[:TYPE]-(galaxykids7wifi),
(tablet)<-[:TYPE] -(galaxytab310wifi3g),
(tablet)<-[:TYPE]-(galaxytab310wifi),
(tablet)<-[:TYPE]-(galaxytab38wifi3g),
(tablet)<-[:TYPE]-(galaxytab38wifi)

我试图删除让节点1连接到它的所有子节点(在这种情况下是产品)和它们之间共享的关系 . 我尝试了很多解决方案,人们说在各种网页上工作,但我继续这样做

'STATEMENT_EXECUTION_ERROR' : 'Node with id 1'

我'm guessing the problem is that the query can't读取了带有 id : 1 的节点,但这没有意义 . 请尽可能帮助我 .

像这样的查询不起作用:

start n=node(1) 
match n-[*]-x 
WITH x 
MATCH x-[r]-() delete x,r

EDIT

以下两个答案也不起作用 . 我正在运行Neo4j 2.0.0 - M06

1)

MATCH (n {id:'1'})<-[r]-x-[ss*0..]-y
WHERE NOT r IN ss  
OPTIONAL MATCH n-[t]->()
FOREACH (s IN ss | DELETE s)
DELETE r,y,t,n

Error: Invalid input 'P' : expected 'r/R'
   "OPTIONAL MATCH n-[t]->()"
     ^

2)

match (n {id : '1'})
optional match n-[r]-x
delete r,x

Invalid input 'o': expected whitespace, comment, a relationship patter, .....
"optional match n-[r]-x"
 ^

2 回答

  • 5

    当您引用节点(1)时,您不是指具有值为1的id属性的节点,而是要求内部标识为1的节点(由Neo4j内部维护,您无法控制分配此值) . 所以这可能是它找不到id为1的节点的原因 .

    如果你是2.0,那么这样的东西可以工作(未测试):

    match (n {id : '1'})
    optional match n-[r]-x
    delete r,x
    
  • 3

    Luanne已经回答了错误,因此提出了问题(如果错误消失,请将其答案标记为已接受),但如果您想通过 (tablet) 删除附加到图表中的所有内容,则还需要更好的模式 . 这样的事情应该适用于您的数据样本

    MATCH (n {id:'1'})<-[r]-x-[ss*0..]-y
    WHERE NOT r IN ss  //I expected relationship uniqueness to ensure that 'r' is not traversed twice, but it didn't so I added an explicit check
    OPTIONAL MATCH n-[t]->()
    FOREACH (s IN ss | DELETE s)
    DELETE r,y,t,n
    

    但是,如果您的图形不是严格意义上的树而子图形附加在其他位置,例如用户 [:OWNS] 同时是galaxytab38wifi和某个打印机,那么用户,所有打印机以及连接到它们的所有内容也将被删除 . 因此,您可能希望进一步限制模式或注意数据的顺序 .

相关问题