在Cypher或NeoModel中获取节点ID和母体ID

发布于 2025-01-19 03:02:45 字数 819 浏览 3 评论 0 原文

我正在使用neoModel,并且有以下模型:

class ForumElement(StructuredNode):
    uid = UniqueIdProperty()
    created_at = DateTimeProperty(default=dt.datetime.utcnow())
    text = StringProperty()
    is_visible = BooleanProperty(default=True)

    picture = Relationship(Picture, 'HAS_PICTURE')
    author = Relationship(User, 'HAS_USER')


class Post(ForumElement):
    title = StringProperty(default="")
    latitude = FloatProperty()
    longitude = FloatProperty()

    tags = Relationship(Tag, 'HAS_TAGS')


class Comment(ForumElement):
    parent = Relationship(ForumElement, 'HAS_PARENT')

使用该代码,我在数据库中具有,在蓝色的地方,我们有“评论”,粉红色我们有“帖子”。

现在,我想获得一个疑问的结果,一对夫妇< parent.uid,childen.uid>,我怎么能得到呢?请注意,评论的父母可能是帖子或其他评论

I'm using neomodel and I have the following models:

class ForumElement(StructuredNode):
    uid = UniqueIdProperty()
    created_at = DateTimeProperty(default=dt.datetime.utcnow())
    text = StringProperty()
    is_visible = BooleanProperty(default=True)

    picture = Relationship(Picture, 'HAS_PICTURE')
    author = Relationship(User, 'HAS_USER')


class Post(ForumElement):
    title = StringProperty(default="")
    latitude = FloatProperty()
    longitude = FloatProperty()

    tags = Relationship(Tag, 'HAS_TAGS')


class Comment(ForumElement):
    parent = Relationship(ForumElement, 'HAS_PARENT')

With that code I have in the database something like the image, where in blue we have "comments" and in pink we have "post".

Now, I would like to have as result of a query a list of couple <parent.uid, childen.uid>, how could I obtain that? Notice that the parent of a Comment could be a Post or another Comment

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

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

发布评论

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

评论(1

似狗非友 2025-01-26 03:02:45

在neo4j上,您可以使用基本的密码查询:

MATCH(c)-[:HAS_PARENT]->(p)
RETURN c.uid, p.uid

Just Match ,根据一种与Label HAS_PARENT return> return 仅UID的模式特性。

当在Python上与NeoModel一起使用时,可以这样称呼:

query = '''MATCH(c)-[:HAS_PARENT]->(p) RETURN c.uid, p.uid'''})
results, meta = db.cypher_query(query, {})

您可以在这样的模拟数据上对其进行测试:

MERGE (mark:COMMENT {uid: "Mark"})
MERGE (lju:COMMENT {uid: "Lju"})
MERGE (praveena:COMMENT {uid: "Praveena"})
MERGE (zhen:POST {uid: "Zhen"})
MERGE (martin:COMMENT {uid: "Martin"})
MERGE (mark)-[:HAS_PARENT]-(lju)
MERGE (lju)-[:HAS_PARENT]-(praveena)
MERGE (praveena)-[:HAS_PARENT]-(zhen)
MERGE (martin)-[:HAS_PARENT]-(zhen)

On neo4j you can use a basic Cypher query like this:

MATCH(c)-[:HAS_PARENT]->(p)
RETURN c.uid, p.uid

Just MATCH according to the pattern of one relationship with label HAS_PARENT and RETURN only the uid properties.

When used with neomodel on python, it can be called like this:

query = '''MATCH(c)-[:HAS_PARENT]->(p) RETURN c.uid, p.uid'''})
results, meta = db.cypher_query(query, {})

You can test it on mock data like this:

MERGE (mark:COMMENT {uid: "Mark"})
MERGE (lju:COMMENT {uid: "Lju"})
MERGE (praveena:COMMENT {uid: "Praveena"})
MERGE (zhen:POST {uid: "Zhen"})
MERGE (martin:COMMENT {uid: "Martin"})
MERGE (mark)-[:HAS_PARENT]-(lju)
MERGE (lju)-[:HAS_PARENT]-(praveena)
MERGE (praveena)-[:HAS_PARENT]-(zhen)
MERGE (martin)-[:HAS_PARENT]-(zhen)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文