Search code examples
orientdb

Convert String Datatype column to "Embeddedlist" datatype in Orientdb


I have a column "userUnits" as string data-type with respect to user and userUnits column having multiple units entered separated by the comma(,) as user1 userUnits are "admin,manager" and user2 userUnits are "admin" as string datatype and I have to convert it into "embeddedlist" data-type as user1 userUnits will be ["admin","manager"] and user2 userUnits will be ["admin"] so how can I convert it for all the users? can anyone help me with this please


Solution

  • I tried to reproduce your problem

    enter image description here

    I used this query

    update user set newProperty=userUnits.asList()
    

    and I got

    enter image description here

    To remove the property userUnits from your records, you could use

    update user remove userUnits
    

    Hope it helps.

    UPDATE

    You can use this javascript function

    var g=orient.getGraphNoTx();
    g.command("sql","CREATE PROPERTY User.newProperty embeddedlist" );
    var users = g.command("sql","select from User");
    for(i=0;i<users.length;i++){
        var record=users[i];
        var id=record.getId();
        var userUnits=record.getProperty("userUnits");
        var array=userUnits.split(",");
        var myvalue='"';
        for(j=0;j<array.length;j++){
            if(j==0)
                myvalue+=array[j]+'"';
            else
                myvalue+=',"'+array[j]+'"';
        }
        g.command("sql","UPDATE " + id + " set newProperty = [ " + myvalue + "]");
    }
    g.command("sql","UPDATE user REMOVE " + userUnits);