0

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

1 Answers1

0

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)
nimrod serok
  • 14,151
  • 2
  • 11
  • 33