2016-03-15 269 views
0

里面一个javascript函数,我有几个疑问,如:结果迭代

sQuery = "select in('E_Edge')[email protected] as id from ?"; 
result = db.command ('sql', sQuery,[givenId]); 

如果我返回的结果,它可以是这样的:

[ 
    { 
    "@type":"d", 
    "@rid":"#-2:0", 
    "@version":0, 
    "id":[ 
    "#35:62", 
    "#35:63", 
    "#35:64", 
    "#35:65", 
    "#35:69" 
    ], 
    "@fieldTypes":"id=z" 
    } 
] 

如果我想获得ID列表,我可以这样做:

var ids= result[0].getProperty("id"); 

如果我回来了,我得到了扩展的ID,即带有这些ID的节点列表。

但是,如果我想遍历它并执行其他操作,ids.length不作为属性存在,则size()不可用。

如何遍历列表?

回答

2

如果你想遍历列表(如果我理解正确的话),用于进行其他操作,你可以试试这个:

var db=orient.getGraph(); 
var result=db.command('sql',"select in('E_Edge')[email protected] as id from Test where @rid='"+rid+"'"); 

for(i=0;i<result.length;i++) 
{ 
    var ids=result[i].getProperty("id"); 
    print(ids); 
} 

如果不是你在找你能解释它更好?

让我知道。

+0

嗨,这不是工作,因为result.length是1 IDS我要遍历的列表,是结果数组内,其中包含1个对象,其中包含一个属性“id”,它是一个数组。而这个最终数组就是我想要迭代的。 –

1

我想你的情况下,采用这种结构:

create class Person extends V 
create class Movie extends V 
create class acts_In extends E 
create class directed extends E 
create class friend extends E 
create class rated extends E 

create property Person.name String 
create property Person.surname String 
create property Movie.title String 

create vertex Person set name="Tom", surname="Hanks" 
create vertex Person set name="Robin", surname="Wright" 
create vertex Person set name="Helen", surname="Hunt" 
create vertex Person set name="Robert", surname="Zemeckis" 
create vertex Person set name="Russell", surname="Crowe" 
create vertex Person set name="Ben", surname="Affleck" 
create vertex Person set name="Kevin", surname="Macdonald" 
create vertex Person set name="John" 
create vertex Person set name="Mark" 
create vertex Person set name="Paul" 
create vertex Person set name="Mel", surname="Gibson" 
create vertex Person set name="Nancy", surname="Meyers" 
create vertex Movie set title="Forrest Gump" 
create vertex Movie set title="Cast Away" 
create vertex Movie set title="State of Play" 
create vertex Movie set title="What Women Want" 

create edge acts_In from (select from Person where name="Tom" and surname="Hanks") to (select from Movie where title="Forrest Gump") 
create edge acts_In from (select from Person where name="Tom" and surname="Hanks") to (select from Movie where title="Cast Away") 
create edge acts_In from (select from Person where name="Robin" and surname="Wright") to (select from Movie where title="Forrest Gump") 
create edge acts_In from (select from Person where name="Robin" and surname="Wright") to (select from Movie where title="State of Play") 
create edge acts_In from (select from Person where name="Helen" and surname="Hunt") to (select from Movie where title="Cast Away") 
create edge acts_In from (select from Person where name="Helen" and surname="Hunt") to (select from Movie where title="What Women Want") 
create edge acts_In from (select from Person where name="Mel" and surname="Gibson") to (select from Movie where title="What Women Want") 
create edge acts_In from (select from Person where name="Russell" and surname="Crowe") to (select from Movie where title="State of Play") 
create edge acts_In from (select from Person where name="Ben" and surname="Affleck") to (select from Movie where title="State of Play") 
create edge friend from (select from Person where name="Mel" and surname="Gibson") to (select from Person where name="Helen" and surname="Hunt") 
create edge friend from (select from Person where name="Ben" and surname="Affleck") to (select from Person where name="Russell" and surname="Crowe") 
create edge directed from (select from Movie where title="What Women Want") to (select from Person where name="Nancy" and surname="Meyers") 
create edge directed from (select from Movie where title="Cast Away") to (select from Person where name="Robert" and surname="Zemeckis") 
create edge directed from (select from Movie where title="Forrest Gump") to (select from Person where name="Robert" and surname="Zemeckis") 
create edge directed from (select from Movie where title="State of Play") to (select from Person where name="Kevin" and surname="Macdonald") 
create edge rated from (select from Movie where title="What Women Want") to (select from Person where name="Paul") 
create edge rated from (select from Movie where title="Cast Away") to (select from Person where name="John") 
create edge rated from (select from Movie where title="Forrest Gump") to (select from Person where name="Mark") 
create edge rated from (select from Movie where title="State of Play") to (select from Person where name="John") 

QUERY:选择谁在Movie@rid 13:2(“绝对阴谋”)

现在,你可以尝试不同的方法行事演员检索您要查找的结果:

  1. 使用简单的SELECT FROM @rid查询:

    CODE

    var db = orient.getGraph(); 
    var givenId = '13:2'; 
    var actorsList=[]; 
    var sQuery = "select from ?"; 
    var result = db.command('sql',sQuery, [givenId]); 
    for(i=0;i<result.length;i++){ 
        var in_acts_In = result[i].getRecord().field('in_acts_In.out'); 
        if(in_acts_In!=null){ 
        var actorsIter=in_acts_In.iterator(); 
        if(actorsIter!=null){ 
         while(actorsIter.hasNext()){ 
         var iteratedActor=actorsIter.next(); 
         actorsList.push(iteratedActor.getRecord()); 
         } 
        } 
        } 
    } 
    return actorsList; 
    

    输出

    CODE

    [ 
        { 
         "@type": "d", 
         "@rid": "#12:1", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Robin", 
         "surname": "Wright", 
         "out_acts_In": [ 
          "#14:2", 
          "#14:3" 
         ], 
         "@fieldTypes": "out_acts_In=g" 
        }, 
        { 
         "@type": "d", 
         "@rid": "#12:4", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Russell", 
         "surname": "Crowe", 
         "out_acts_In": [ 
          "#14:7" 
         ], 
         "in_friend": [ 
          "#16:1" 
         ], 
         "@fieldTypes": "out_acts_In=g,in_friend=g" 
        }, 
        { 
         "@type": "d", 
         "@rid": "#12:5", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Ben", 
         "surname": "Affleck", 
         "out_acts_In": [ 
          "#14:8" 
         ], 
         "out_friend": [ 
          "#16:1" 
         ], 
         "@fieldTypes": "out_acts_In=g,out_friend=g" 
        } 
    ] 
    
  2. expand()您的查询的结果

    var db=orient.getGraph(); 
    var givenId = '13:2'; 
    var actorsList=[]; 
    var result=db.command('sql',"select expand(in('acts_In')) from ?",[givenId]); 
    for(i=0;i<result.length;i++) 
    { 
        var actor=result[i].getRecord(); 
        actorsList.push(actor);; 
    } 
    return actorsList; 
    

    OUTPUT

    [ 
        { 
         "@type": "d", 
         "@rid": "#12:1", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Robin", 
         "surname": "Wright", 
         "out_acts_In": [ 
          "#14:2", 
          "#14:3" 
         ], 
         "@fieldTypes": "out_acts_In=g" 
        }, 
        { 
         "@type": "d", 
         "@rid": "#12:4", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Russell", 
         "surname": "Crowe", 
         "out_acts_In": [ 
          "#14:7" 
         ], 
         "in_friend": [ 
          "#16:1" 
         ], 
         "@fieldTypes": "out_acts_In=g,in_friend=g" 
        }, 
        { 
         "@type": "d", 
         "@rid": "#12:5", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Ben", 
         "surname": "Affleck", 
         "out_acts_In": [ 
          "#14:8" 
         ], 
         "out_friend": [ 
          "#16:1" 
         ], 
         "@fieldTypes": "out_acts_In=g,out_friend=g" 
        } 
    ] 
    
  3. 使用(在我的情况rid与值13:2)的输入功能参数:

    CODE

    var db=orient.getGraph(); 
    var result=db.command('sql',"select in('acts_In')[email protected] as id from "+rid); 
    for(i=0;i<result.length;i++) 
    { 
        var idList=result[i].getProperty("id"); 
    } 
    return idList; 
    

    输出

    [ 
        { 
         "@type": "d", 
         "@rid": "#12:1", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Robin", 
         "surname": "Wright", 
         "out_acts_In": [ 
          "#14:2", 
          "#14:3" 
         ], 
         "@fieldTypes": "out_acts_In=g" 
        }, 
        { 
         "@type": "d", 
         "@rid": "#12:4", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Russell", 
         "surname": "Crowe", 
         "out_acts_In": [ 
          "#14:7" 
         ], 
         "in_friend": [ 
          "#16:1" 
         ], 
         "@fieldTypes": "out_acts_In=g,in_friend=g" 
        }, 
        { 
         "@type": "d", 
         "@rid": "#12:5", 
         "@version": 3, 
         "@class": "Person", 
         "name": "Ben", 
         "surname": "Affleck", 
         "out_acts_In": [ 
          "#14:8" 
         ], 
         "out_friend": [ 
          "#16:1" 
         ], 
         "@fieldTypes": "out_acts_In=g,out_friend=g" 
        } 
    ] 
    

希望它可以帮助

+0

Hi Mihai,你有机会尝试这个功能吗? – LucaS