简体   繁体   中英

ArangoDB: Last of collection in path

I have a couple of document collections containing assets and asset types and an edge collection for creating:

  • hierarchy of assets types ( dmr-radio is_a radio is_a asset )
  • hierarchy of the contents of assets ( the vehicle contains the radio )
  • links to what type of asset an asset is ( the vehicle is_a vehicle , the radio is_a dmr-radio ).

在此处输入图像描述

I am trying to find all radios that are in a vehicle. I was thinking of using a k Paths graph query to get all the assets that are a type of radio (see below), but I am struggling to find a way to then return only the asset that is the radio (the last vertex that is in the assets collection).

Is there a way to get the last vertex in a path from a certain collection and is there a better way to do this query?

FOR path
  IN 1..100 OUTBOUND K_PATHS
  'assets/493761' TO 'assetTypes/radio'
  GRAPH assetRelationships
  RETURN path

I have managed to obtain what I wanted using a User-defined Function

/**
 * ArangoDB User-defined Function for getting the last
 * vertex of a collection type in a graph path
 *
 * @param {Path} path ArangoDB graph path to search
 * @param {string} type Name of collection that the returned
 *   vertex should be from
 */
const lastOf = (path, type) => {                                                                                                                                                                                                                                                                                                                                                           
  type += '/';
  
  for (let i = path.vertices.length - 1; i => 0; i--) {
    if (path.vertices[i]._id.startsWith(type)) {
      return path.vertices[i];
    }
  }

  return null;
};

which I am registering when I set up my database connection

import { Database } from 'arangojs';

const database = new Database({
 ...
});

...

await database.createFunction('MY_FUNC::LAST_OF', lastOf.toString(), true);

and am then using in a AQL query

FOR path
  IN 1..100 OUTBOUND K_PATHS
  'assets/493761' TO 'assetTypes/radio'
  GRAPH assetRelationships
  RETURN MY_FUNC::LAST_OF(path, 'assets')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM