AVK
AVK

Reputation: 645

Getting a list item using record's attribute as an index

How can one get access to an item inside a list using one of the attributes as an index into that list?

Here's the test schema:

create class Test extends V
insert into Test content {"items": [ { "name": "Item 1" }, { "name": "Item 2" } ], "selected": 1 }

Suppose new records RID is #100:0, then the following queries do not work:

select items[ @this.selected ].name from #100:0
select items[ eval('@this.selected') ].name from #100:0

What am I doing wrong? Im on orient 2.1.8.

Upvotes: 1

Views: 131

Answers (2)

LucaS
LucaS

Reputation: 1418

I have this simple structure:

enter image description here

To retrieve the results you're looking for, I used this query:

select items[$i.sel].name from Test
let $i = (select eval('selected') as sel from #12:0)

Output:

----+------+------
#   |@CLASS|items
----+------+------
0   |null  |Item 1
1   |null  |Item 3
----+------+------

EDITED

Or this:

select items[$i.sel].name from Test
let $i = (select eval('selected') as sel from #12:1)

Output:

----+------+------
#   |@CLASS|items
----+------+------
0   |null  |Item 2
1   |null  |Item 4
----+------+------

2nd EDIT

Query 1:

select items.name[$i.sel] from Test
let $i = (select *, eval('selected') as sel from 12:0)
where @this in $i

Output:

----+------+------
#   |@CLASS|items
----+------+------
0   |null  |Item 1
----+------+------

Query 2:

select items.name[$i.sel] from 12:1
let $i = (select eval('selected') as sel from 12:0)

Output:

----+------+------
#   |@CLASS|items
----+------+------
0   |null  |Item 3
----+------+------

Query 3:

select items[$i.selected].name from Test let $i = selected unwind items

Output:

----+------+------
#   |@CLASS|items
----+------+------
0   |null  |Item 1
1   |null  |Item 2
2   |null  |Item 3
3   |null  |Item 4
----+------+------

Hope it helps

Upvotes: 2

AVK
AVK

Reputation: 645

Seems like this is the solution which works for multiple records:

select items[$i].name from Test let $i = eval('selected')

NOTE: However, it still makes sense if attributes could be used directly to index elements in queries that reference embedded lists. This might probably be available in later versions of OrientDB

Upvotes: 1

Related Questions