Search code examples
orientdb

OrientDB: is it possible to add/set fields to an existing class on the fly?


OrientDB beginner here!

I am setting up a graph to simulate a social network. I have started by creating a new class, Person, and I have several persons on file I want to enter.

But my problem is that all fields are not present all the time. My first Person I want to enter have FirstName and FamilyName. The second entry have those two but also Age.

But when i run

select from Person

my result pane only show FirstName and FamilyName of my two records. My assumption was that I would see all three columns, FirstName, FamilyName and Age with Age being empty for the first entry.

So I assume that adding a new "field" or property on the fly is not accepted.

What would then be the best practice to achieve add the extra property on the fly, given that I don't know all the possible fields before I start (no schema)


Solution

  • If you first execute following statements:

    create class Person extends V
    create vertex Person set FirstName = "Jon", FamilyName = "Doe"
    create vertex Person set FirstName = "Johanna", FamilyName = "Doe", Age = 30
    

    and after that execute select, output will be something like this:

    orientdb {db=DoctorDB}> select * from Person
    
    
    +----+-----+------+---------+----------+----+
    |#   |@RID |@CLASS|FirstName|FamilyName|Age |
    +----+-----+------+---------+----------+----+
    |0   |#91:0|Person|Jon      |Doe       |    |
    |1   |#92:0|Person|Johanna  |Doe       |30  |
    +----+-----+------+---------+----------+----+ 
    

    and then:

    update #92:0 set Sex = "Female"
    

    now select outputs:

    orientdb {db=DoctorDB}> select * from Person
    
    +----+-----+------+---------+----------+----+------+
    |#   |@RID |@CLASS|FirstName|FamilyName|Age |Sex   |
    +----+-----+------+---------+----------+----+------+
    |0   |#91:0|Person|Jon      |Doe       |    |      |
    |1   |#92:0|Person|Johanna  |Doe       |30  |Female|
    +----+-----+------+---------+----------+----+------+