dargolith
dargolith

Reputation: 311

OrientDB SQL: Include rids from connected vertices

I have one vertex "A" connected to vertices "B" and "C".

I want to query for A with an appended list of the record-ids of B and C.

Example of how properties could look:

A:
{
   "@rid": "#1:0",
   "property": "ValueForA"
}

B:
{
   "@rid": "#2:0",
   "property": "ValueForB"
}

C:
{
   "@rid": "#2:1",
   "property": "ValueForC"
}

I want a query for A that gives me:

{
    "@rid": "#1:0",
    "property": "ValueForA",
    "connected": ["#2:0", "#2:1"]
}

I would prefer to be able to do this with only projections. It works fine for any custom field in B and C, but for @rid I get a link (why this inconsistency?).

I've tried:

SELECT @rid, *, out().@rid as connected FROM #1:0

Which gives me exactly what I want but the rids in the list are links to the records, not only rids.

And

SELECT @rid, *, out()[email protected]() as connected FROM #1:0

Which gives:

{
    "@rid": "#1:0",
    "property": "ValueForA",
    "connected": "[#2:0, #2:1]"
}

Where the connected property is just a string.

There's probably (hopefully?) a reason for this inconsistency of @rid and other properties. As I wrote, if @rid instead was "id", the first example would work fine. Regardless, I would like to know an easy way to do this, because I feel it should be easy to do it.

Update

It was misleading to add id as property example in the examples above, since it could somehow hint that it was unique (though I never said that), so I removed it. I only added them to keep the 3 records apart. However, record-id is what is being used as unique id to separate the vertices above, so using any properties of the record is not possible. To clarify further, the record can have arbitrary properties. Not only the example ones given above.

Again, first question is if it is possible using projections?

Further comments: I find it really strange that I could easily do it if I made a property "rid" holding the exact same record id as @rid for each record. Why would I have to do that (I will not do that) in order to make it easy to use though.

Update2

I also would like to be able to target several vertices at once and append connections for each of them. I do this already, using projections, for many properties (but not @rid).

Upvotes: 0

Views: 109

Answers (1)

Michela Bonizzi
Michela Bonizzi

Reputation: 2632

The fact that the query return the @rid like a link is pretty normal, a possible way to do it. it could be this one:

select id, property, $a from #21:0
let $a = (select out() from #21:0 where $parent.current.id = id)

this is the output:

enter image description here

Hope it helps.

Regards

Upvotes: 0

Related Questions