Neo4j 中链接列表的字段数组

Array of fields to linked list in Neo4j

我正在尝试根据数组中的数据创建一系列链接的节点。我有头节点,我想在其中附加链接节点,但是,当使用 UNWIND 或 FOREACH 时,我最终将头节点单独链接到每个链接节点,而不是像链表一样。

$locations 包含一个对象数组,其中包含创建我需要的节点所需的信息。我之前无法创建节点并进行收集,因为我没有独特的方法来获取我刚刚创建的那些特定类型的节点,因为它应该基于它们链接到的头节点。

CREATE (head:Game:Trail{
  GUID: apoc.create.uuid(),
  creationDate: datetime(),
  title: $title,
  description: $description
})

选项 1

FOREACH (trail IN $locations |
  CREATE (a:Pin:Trail{ GUID: apoc.create.uuid(), creationDate: datetime(), text: trail.hint.text, lat: trail.lat, lng: trail.lng })
  MERGE (head)-[:LEADS_TO]->(a)
  MERGE (a)<-[r1:IS_ABOUT]-(image:Image:Media{GUID: apoc.create.uuid(), filename: trail.blobFile.filename})
    ON CREATE SET a.imageURL = 'https://......../' + image.GUID + '.' + trail.blobFile.extension
  SET head = a
)

选项 2

UNWIND $locations as trail
CALL apoc.lock.nodes([head])
WITH head, trail

MATCH (head)-[:LEADS_TO*0..]->(end)
  WHERE NOT (end)-[:LEADS_TO]->()

CREATE (end)-[:LEADS_TO]->(a:Pin:Trail{ GUID: apoc.create.uuid(), creationDate: datetime(), text: trail.hint.text, lat: trail.lat, lng: trail.lng })

输出应该是这种格式

(头)-[LEADS_TO]->(节点)-[LEADS_TO]->(节点2)

但是,我尝试过的两个选项都产生了相同的输出,我最终得到的格式如下:

(head)-[LEADS_TO]->(node), (head)-[LEADS_TO]->(node2)\

非常感谢任何帮助!谢谢!

我在这里度过了一个 APOC 日 - 可能有一种 Cypher 方法可以做到这一点,但给出了一些虚拟数据:

:param locations => [
    { hint: { text: 'first stop' }, lat: 1, long: 1 },
    { hint: { text: 'second stop' }, lat: 2, long: 1 },
    { hint: { text: 'last stop' }, lat: 3, long: 1 }
]

以下 APOC-heavy Cypher 块将创建节点,从具有 apoc.nodes.link 的节点创建一个 linked 列表,然后 link 链中的第一个节点到'head' 节点:

CREATE (head:Game:Trail {
    GUID: apoc.create.uuid(),
    creationDate: datetime(),
    title: 'Some title',
    description: 'Some description'
})
WITH head, [trail in $locations | { GUID: apoc.create.uuid(), creationDate: datetime(), text: trail.hint.text, lat: trail.lat, long: trail.long }] as trails
CALL apoc.create.nodes(['Pin', 'Trail'], trails) YIELD node
WITH head, collect(node) as nodes
CALL apoc.nodes.link(nodes, 'LEADS_TO')
WITH head(nodes) as firstNode, head
MERGE (head)-[:LEADS_TO]->(firstNode)