简体   繁体   English

ArangoDB:路径中的最后一个集合

[英]ArangoDB: Last of collection in path

I have a couple of document collections containing assets and asset types and an edge collection for creating:我有几个包含资产和资产类型的文档 collections 以及用于创建的边缘集合:

  • hierarchy of assets types ( dmr-radio is_a radio is_a asset )资产类型的层次结构( dmr-radio is_a radio is_a asset
  • hierarchy of the contents of assets ( the vehicle contains the radio )资产内容的层次结构(车辆contains收音机
  • links to what type of asset an asset is ( the vehicle is_a vehicle , the radio is_a dmr-radio ).链接到资产是什么类型的资产(车辆is_a vehicle收音机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).我正在考虑使用k 路径图查询来获取所有属于无线电类型的资产(见下文),但我正在努力寻找一种方法来仅返回无线电资产(最后一个顶点是在资产集合中)。

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我已经设法使用用户定义的 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然后在 AQL 查询中使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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