title: Accessing Connected Vertices in OrientDB Javascript Functions
body: <p>I'm attempting to use the build in Functions in OrientDB studio to group Workstations that aren't in use by a Person. The query to get these vertices works fine but I'm trying to avoid Traverse as it is very slow - too slow to be used in production. Instead of iterating through each free station and grouping it together with all it's neighbours, keeping each grouped 'named' with the smallest @rid in the set.</p>
  
  <pre><code>var groups = {};            //The list of groups of workpoints. The key is the lowest RID in the group
  var mappedDesks = {};       //Every desk's RID is in this object with it's matching value being the group name they're in    
  
  //Get all Workpoints that don't have a Locale CURRENTLY_LOCATED_ON them
  var freeDesks = db.query("SELECT FROM Workpoint WHERE @rid NOT IN (SELECT @rid FROM (SELECT EXPAND(OUT('CURRENTLY_LOCATED_ON').OUT('LOCATED_ON')) FROM Person) WHERE @class = 'Workpoint')");    
  
  //Iterate through all vacant Workpoints
  for (var j=0; j &lt; freeDesks.length; j++){
      var baseNodeRid = freeDesks[j].getRecord().getIdentity().toString();                    // The RID of the Workpoint
      var baseNodeNumber = parseFloat(baseNodeRid.replace("#", "").replace(":","."));         // The RID converted to a number for comparisons. The lower RID takes precedence
      var baseSanitized = baseNodeRid.replace(":","-")                                        // Keys cannot contain colon so they are replaced with a dash    
  
      if (freeDesks[j].getRecord().field("out_NEIGHBOUR_OF") == null ) {
          // Desks without neighbours can be put in a group on their own
          groups[baseSanitized] = new Array();
          groups[baseSanitized].push(baseNodeRid);
          mappedDesks[baseSanitized] = baseSanitized;
      } else {
          //Iterate over all the desk's neighbours
          for (var n = 0; n &lt; freeDesks[j].getRecord().field("out_NEIGHBOUR_OF").length; n++){          
  
              //Convert the neighbour's RID to a number too
              var nSanitized = n.replace(":","-");
  
              if (parseFloat(n.replace("#", "").replace(":",".")) &gt; baseNodeNumber ){
                  //The neighbour's node group is larger than the current one. This needs to be merged into the group with the smaller rid    
  
                  //Move the desks from the neighbour's group into the base's group. If it has none then do nothing
                  var nGroup = groups[mappedDesks[nSanitized]]
                  if ( nGroup != null) {
                      groups[baseSanitized] = groups[baseSanitized].concat(nGroup);
  
                      //Change the mapping of each moved desk to the base's
                      for (var g = 0; g &lt; nGroup.length; g++){
                          mappedDesks[nGroup[g]] = baseSanitized;          
                      }
                  }    
  
  
  
                  //Delete the reference to the old group
                  delete groups[mappedDesks[nSanitized]];    
  
                  //Update the mappings for the desks dealt with
                  mappedDesks[nSanitized] = baseSanitized;
                  mappedDesks[baseSanitized] = baseSanitized;    
  
              } else {
                  // The neighbour is lower than the current desk so the desk should be merged into the neighbour's group
                  mappedDesks[baseSanitized] = nSanitized;
                  groups[nSanitized].push(baseNodeRid);
              }
          }
      }    
  }    
  
  return groups;
  </code></pre>
  
  <p>My problem comes from accessing a vertex's neighbours. It correctly determines whether there are neighbours in the if statement <code>return freeDesks[j].getRecord().field("out_NEIGHBOUR_OF")</code> but I want to be able to get each neighbour's @rid so I can sort the @rids into groups.</p>
  
  <p><code>freeDesks[j].getRecord().field("out_NEIGHBOUR_OF")</code> returns the edge record but I dont seem to be able to get the "in" or "out" fields using the field() method (not found on this object) or accessing it as an array []. </p>
  
  <pre><code>[
      {
          "@type": "d",
          "@rid": "#34:18176",
          "@version": 6,
          "@class": "NEIGHBOUR_OF",
          "out": "#16:13",
          "in": "#16:1408",
          "@fieldTypes": "out=x,in=x"
      }
  ]
  </code></pre>
  
  <p>Can you help be get a list/array of the neighbour @rids so I can iterate over them with the rest of the code?</p>
  
  <p>Cheers!</p>