在密码中进行foreach查询以合并类似的节点

时间:2014-11-18 17:10:15

标签: neo4j cypher

我试图弄清楚如何运行我的数据库,收集相似的节点,然后将它们合并到一个节点中,并将先前的关系重定向到新创建的节点。 我基本上创建了一堆节点,其中部分或全部属性都有信息,如下所示:

MERGE (company:Company{Name: 'Ford'})
MERGE (person:Person{Name: 'me'})
MERGE (car:Car{Make:'Ford', Model:'Aerostar', Color:'Blue', Transmission:'Auto'})

但我意外地复制了本应合并的节点,而是创建了新节点:

MERGE (car:Car{Make:'Ford', Model:'Aerostar', Color:'', Transmission:''})
MERGE (car:Car{Make:'Ford', Model:'Aerostar', Color:'Blue', Transmission:''})
MERGE (car:Car{Make:'Ford', Model:'Aerostar', Color:'Blue', Transmission:'Auto'})
MERGE (person)-[:drives]->(car)-[:parent_company]->(company)

所以,我想要做的是拿出我意外创建的三个(car)个节点,合并它们的所有属性,删除额外节点创建的额外关系并更正路径,所以(me)会只有一个[:drives]关系连接到由(car)个关系连接的单个[:parent_company]

这是我尝试过的,但无法弄清楚:

MATCH p=(car:Car{Make:'Ford', Model:'Aerostar'})<-[:drives]-(person:Person{Name:'me'})
FOREACH (car in nodes (p) | SET Car.Color: 'Blue', Car.Transmission:'Auto')
/////This is where I'm stuck

编辑:另一次尝试(我得到&#34; r已经宣布错误):

START n = node(3) //node id for complete 'aerostar' node
WITH n
MATCH (company)<-[:parent_company]-(car:Car{Make:'Ford', Model:'Aerostar')<-[r:drives]-(person)
WITH n, company, r, car, person
MERGE (person)-[r]->(n)-[:parent_company]->(company)
DELETE car

2 个答案:

答案 0 :(得分:1)

[EDITED]

这对你有用吗?

MATCH (car:Car{Make:'Ford', Model:'Aerostar'})<-[d:drives]-(person:Person{Name:'me'})
DELETE d, car
WITH person
CREATE (car:Car{Make:'Ford', Model:'Aerostar', Color:'Blue', Transmission:'Auto'})<-[d:drives]-(person);

答案 1 :(得分:0)

我终于想出了如何做到这一点:

MATCH (car:Car{Make:'Ford', Model:'Aerostar'})<-[d:drives]-(person:Person{Name:'me'})
WITH car,d
SKIP 1
DELETE car,d