0
votes

Let us assume that two classes exists like Posts and Comments in OrientDB, and Posts class has a linklist-typed property named Comments.

How can I get post @rid and answer json-converted data as a last property of the result like below:

@rid      answers
-----     -------
#13:1     [{"content":"test answer"},{"content":"test answer 2"}] 
#13:2     [{"content":"test answer22"},{"content":"test answer 23"}] 
1
Hi, is your schema like this ? Post <----- Comment where 'answer' is a property of class Comment ? - LucaS
Sorry for that, each comment is an answer to the post. Post has a property named "Answers" which is linklist. We can replace Answers with Comments due to same meaning in the context. - Cengiz Poyraz

1 Answers

0
votes

I have tried with http://orientdb.com/docs/2.0/orientdb.wiki/SQL-Methods.html#tojson but I was not lucky.

You can use this javascript function with one parameter (rid)

var g=orient.getGraph();
var comments=g.command("sql","select expand(comments) from "+ rid);
var answer="[";
for(j=0;j<comments.length;j++){
    if(j==0)
        answer=answer + '{"content":"'+ comments[j].getProperty("name")+'"}';
    else
        answer=answer + ',{"content":"'+ comments[j].getProperty("name")+'"}';
}
return answer+"]";

Using the following command

select @rid, myFunction(@rid) as answer from Posts