结果迭代在OrientDB Javascript服务器端函数内部

时间:2016-03-15 14:19:52

标签: javascript function server-side orientdb

在javascript函数中,我有一些查询,例如:

sQuery = "select in('E_Edge').@rid 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 个答案:

答案 0 :(得分:3)

如果您希望迭代列表(如果我理解正确)进行其他操作,您可以尝试:

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

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

如果不是您要找的是什么,您可以更好地解释一下吗?

让我知道。

答案 1 :(得分:2)

我尝试了这种结构:

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;
    

    <强>输出

    [
        {
            "@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()您的查询结果:

    <强> CODE

    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;
    

    <强>输出

    [
        {
            "@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').@rid 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"
        }
    ]
    
  4. 希望有所帮助