Search code examples
orientdb

Query result with nested vertices


[OrientDB beginner here..]

I have a data structure like the following:

User <[PostEdge]- Post <[CommentEdge]- Post
                     ^[CommentEdge]- Post
                     ^[CommentEdge]- Post

So, a post is related to a user via PostEdge, and another post may be related to a post via CommentEdge.

I'm trying to query for a data structure that gives me something like the following:

[
    {
    content: "",
    comments: [{content: ""}, {content: ""}, {content: ""}],
    poster: {name: ""}
    }
]

The closest I've gotten so far is..

SELECT uuid, content, created_on, IN('CommentEdge') as comments, in('PostEdge') as poster
        FROM (SELECT expand(out('PostEdge')) FROM #28:0)
        WHERE outE('CommentEdge').size() = 0

This returns me something like..

[
    {content: "", comments: [#12:0, #13:0, #14:0], poster: [#15:0]}
]

I've spent a good day trying to figure this out and I'm coming up short. That, and I'm fairly certain that the 'FROM (SELECT.....` part isn't a good idea. So, first, is there a function or something that I'm missing that includes those records rather than just gives back the record ID? Second, is there a better way of doing what I'm trying to do? (Which I assume there is, I'm just not there yet in know-how).


Solution

  • So, what I was looking for was a 'fetchplan'. This ended up solving my issue.

    https://orientdb.com/docs/2.2/Fetching-Strategies.html

    Essentially, fetchplan is an instruction that tells the query to expand/traverse linked documents.

    My close-to-end result is like..

    SELECT content, uuid, created_on, @rid, in('CommentEdge') as comments
    FROM (SELECT expand(out('PostEdge')) FROM :userId WHERE outE('CommentEdge').size() = 0 )
    FETCHPLAN comments:1