如何将节点属性转换为虚拟节点并生成[虚拟]关系?

发布于 2025-01-11 03:36:58 字数 1004 浏览 0 评论 0原文

我目前正在学习密码,但试图找出如何完成以下任务。

我的数据

Neo4j 中有一组具有属性的节点。这些属性很少使用,因此拥有实际的节点没有多大意义。我下面的示例是具有名称和大陆属性的国家/地区节点列表。我们不经常使用大陆,但它仍然是为了提供信息而存在。

[
    {
        name: "Australia",
        continent: "Oceania"
    },
    {
        name: "Canada",
        continent: "North America"
    },
    {
        name: "New Zealand",
        continent: "Oceania"
    },
    {
        name: "United States",
        continent: "North America"
    }
]

我想要的

我们希望生成的是每个大陆的虚拟节点+关系,这样我们就可以看到哪些国家属于哪些大陆。因此,每个大陆一个节点,每个国家一个节点,具有从单个大陆到其内国家的多种关系。

图像我想要的节点/关系

我所拥有的

到目前为止我所拥有的如下 - 但它只是为我找到的每个大陆值提供了断开连接的大陆节点,而不将它们分组。

Match (c:Country)
CALL apoc.create.vNode(['Continent'],{name:c.continent}) yield node as s
Return c, apoc.create.vRelationship(s,'HAS_COUNTRY',{},c), s

如何使用 cypher 进行查询以在特定节点上生成虚拟节点、关系和组?

I'm currently learning cypher but trying to figure out how to accomplish the following.

My data

I have a set of nodes in Neo4j that have properties. These properties are seldom used and so having actual nodes does not make much sense. My example below is a list of country nodes with a property of name and continent. We don't often use continent, but it's still there for informational purposes.

[
    {
        name: "Australia",
        continent: "Oceania"
    },
    {
        name: "Canada",
        continent: "North America"
    },
    {
        name: "New Zealand",
        continent: "Oceania"
    },
    {
        name: "United States",
        continent: "North America"
    }
]

What I want

What we're looking to generate is a virtual node + relationship for each continent so we can see which countries belong to which continents. So, one node per continent and one node per country with multiple relationships from a single continent to the countries within.

image of node/relationships I want

What I have

What I have so far is below - but it's just gives me disconnected continent nodes for each of the continent values it finds without grouping them.

Match (c:Country)
CALL apoc.create.vNode(['Continent'],{name:c.continent}) yield node as s
Return c, apoc.create.vRelationship(s,'HAS_COUNTRY',{},c), s

How can I query with cypher to generate virtual nodes and relationships and group on a specific node?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2025-01-18 03:36:58

通过您的查询,您每行生成一个大陆,因此生成 4 个大陆,这就是您看到断开连接的大陆的原因。

您需要将同一大陆的国家/地区分组在一起,并仅在大陆上为它们创建

MATCH (c:Country)
WITH c.continent AS continent, collect(c) AS countries
CALL apoc.create.vNode(['Continent'],{name: continent}) yield node AS s
UNWIND countries AS c
RETURN c, s, apoc.create.vRelationship(s,'HAS_COUNTRY',{},c)

在此处输入图像描述

With your query, you generate one continent per row and thus 4 of them, that's why you see disconnected continents.

You need to group countries of the same continent together and create on continent only for them

MATCH (c:Country)
WITH c.continent AS continent, collect(c) AS countries
CALL apoc.create.vNode(['Continent'],{name: continent}) yield node AS s
UNWIND countries AS c
RETURN c, s, apoc.create.vRelationship(s,'HAS_COUNTRY',{},c)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文