proper way to add extra information to documents (ODocument), Java API, oDB 2.1.2

66 views
Skip to first unread message

Mihai Ocneanu

unread,
Dec 2, 2015, 4:58:20 AM12/2/15
to OrientDB
Hello,

I have a vertex with some attributes and some edges, and I need to retrive it back as JSON, with the extra requirement that I also want to expand some edges.

Code is something like this:

OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>("select from ? ");
List<ODocument> result = graphFactory.getDatabase().command(query).execute(myId);


and I get the result as JSON with:

result.get(0).toJson();



If I try to expand the edges like:

select *, expand(out('E_SomeEdge')) as items from ?

the query only gives back the expand result ignoring the * or any specifically given fields.



If I try to:
  1. retrieve the original result unchanged,
  2. manually get the expanded result,
  3. convert the expanded result to json,
  4. set it into the original result,
  5. finally convert the object to json,

I'll end up with escaped quotation marks for the expanded bit, due to the double JSON conversion - which then I also have to clean up....



To me, this probably means I may not be using the API as intended.



What's the proper way to achieve what I'm after?


Thanks,

Mihai

Luigi Dell'Aquila

unread,
Dec 2, 2015, 5:26:43 AM12/2/15
to orient-...@googlegroups.com
Hi Mihai,

you have two solutions:
1. use fetchplan: doc.toJson("fetchPlan:*:3")

2, use UNWIND:

select *, items.* from (select *, out('E_SomeEdge') as items from ? UNWIND items)

Luigi

--

---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mihai Ocneanu

unread,
Dec 2, 2015, 6:49:00 AM12/2/15
to OrientDB
Hi Mihai,

you have two solutions:
1. use fetchplan: doc.toJson("fetchPlan:*:3")

2, use UNWIND:

select *, items.* from (select *, out('E_SomeEdge') as items from ? UNWIND items)

Luigi

Thanks for the ideas, Luigi.

First method works perfectly and it does what I was looking for.

The second method will also expand/unwind the edges, but if you have for instance 2 edges you are unwinding, then the query will give back 2 results, with the initial vertex the same, and the expanded part different, for each unwinded vertex. Also, in this case, the atribute names for the unwinded vertex are concatenated with the alias, such as ' itemsname '.

Luigi Dell'Aquila

unread,
Dec 2, 2015, 6:58:48 AM12/2/15
to orient-...@googlegroups.com
Hi Mihai,

exactly, the two methods are not equivalent in how they return results.
In case 2 you can repliace the prefix with a custom one, using AS <alias>

Thanks

Luigi


--
Reply all
Reply to author
Forward
0 new messages