Recover all embedded records in any class

51 views
Skip to first unread message

Academia Learning Centro de inglés y de formación

unread,
Nov 8, 2016, 6:56:46 AM11/8/16
to OrientDB
Hello, I wanted to ask a question regarding the inscribed objects.

Is there a way to retrieve all the objects of a class, for example class City and retrieve all the City records, including those inscribed in other records?


Thanks.

user.w...@gmail.com

unread,
Nov 8, 2016, 7:00:47 AM11/8/16
to OrientDB
Hi,

can you explain it better? 
can you give me an example of your structure?

Thx in advance.

Regards,
Michela

Academia Learning Centro de inglés y de formación

unread,
Nov 8, 2016, 9:10:33 AM11/8/16
to orient-...@googlegroups.com
Hi, thank you for answering me.
An example:

{
    "result": [
        {
            "@type": "d",
            "@rid": "#21:0",
            "@version": 1,
            "@class": "Curso",
            "City": {
                "@type": "d",
                "@version": 0,
                "@class": "City",
                "name": "Valenzuela"
            },
            "name": "curso 1"
        },
        {
            "@type": "d",
            "@rid": "#25:0",
            "@version": 1,
            "@class": "City",
            "name": "bolaños"
        },
        {
            "@type": "d",
            "@rid": "#26:0",
            "@version": 1,
            "@class": "City",
            "name": "almagro"
        },
        {
            "@type": "d",
            "@rid": "#25:0",
            "@version": 1,
            "@class": "Country",
            "City": {
                "@type": "d",
                "@version": 0,
                "@class": "City",
                "name": "Madrid"
            },
            "name": "Spain"
        },
    ]

}

I want to get all the cities, in total 4.
But I do not know what classes are embedded
I need something like:

Select * from City

Select * from V where @class = 'City'


And get the 4 cities (2 records and 2 embedded)

thanks

alessand...@gmail.com

unread,
Nov 8, 2016, 9:46:47 AM11/8/16
to OrientDB
Hi,
you could use this query

select expand($c)
let $a
= ( select expand(City) from v),
$b
= ( select from city ),
$c
= unionAll($a,$b)

Best regards,
Alessandro
Message has been deleted
Message has been deleted

Academia Learning Centro de inglés y de formación

unread,
Nov 9, 2016, 3:42:53 AM11/9/16
to OrientDB
Thank you,

One last question, if instead of using the graph model, I use the document model, where classes do not inherit from V.
How could I do this done previously (get all records embedded and not)? 

alessand...@gmail.com

unread,
Nov 9, 2016, 6:28:28 AM11/9/16
to OrientDB
Hi,
I think there isn't such command available to get all your documents with a query.
Could be useful to you a javascript function?

Alessandro

Academia Learning Centro de inglés y de formación

unread,
Nov 9, 2016, 6:42:01 AM11/9/16
to OrientDB
Hi, thanks!!

I mean get all the records of the city class just like in the previous example but instead of using the graph model using the documents model (the classes are not inheriting from V)

alessand...@gmail.com

unread,
Nov 9, 2016, 10:56:00 AM11/9/16
to OrientDB
Hi,
you could use this javascript function

var db=orient.getDatabase();
var classes=db.query('select name from (select expand(classes) from metadata:schema) where name not in ["_studio","OUser","OSchedule","E","V","OFunction","OIdentity","OTriggered","ORole","ORestricted","OSequence"]');

var result=[];

for (var i = 0; i < classes.length; i++) {
   
var className = classes[i].field('name');
   
if(className=="City"){
     
var cities=db.query("select from " + className);
     
for (var j = 0; j < cities.length; j++) {
          result
.push(cities[j]);
     
}
   
}
   
else{
     
var cities=db.query("select expand(City) from " + className);
     
for (var j = 0; j < cities.length; j++) {
         result
.push(cities[j]);
     
}
   
}
}
return result;

Hope it helps.

Academia Learning Centro de inglés y de formación

unread,
Nov 10, 2016, 3:13:39 AM11/10/16
to OrientDB
OK thanks. One last question, in the document model, can also be inherited from class V?
If so then it would be easier to find the city class, right? With the query of the previous example:
Select expand (City) from v

Or in the document model this is not possible?

Thank you!

Academia Learning Centro de inglés y de formación

unread,
Nov 10, 2016, 3:29:59 AM11/10/16
to OrientDB

Hi, I just tried and yes, inheriting from class V, you can get all classes City, in the documents model just like in the graphs using the query:
Select expand ($ c)
Let $ a = (select expand from v),
$ B = (select from City),
$ C = unionAll ($ a, $ b)

alessand...@gmail.com

unread,
Nov 10, 2016, 3:39:45 AM11/10/16
to OrientDB
From previous versions to 2.2 if you created a database as document you had not the classes V and E.
Instead starting from version 2.2 if you create a document database you have the classes V and E, and if your classes inherit from class V, you have a graph model otherwise you have a document model.

Best regards.
Reply all
Reply to author
Forward
0 new messages