[英]ArangoDB: Last of collection in path
我有幾個包含資產和資產類型的文檔 collections 以及用於創建的邊緣集合:
is_a
radio is_a
asset )contains
收音機)is_a
vehicle ,收音機is_a
dmr-radio )。我正在嘗試查找車輛中的所有收音機。 我正在考慮使用k 路徑圖查詢來獲取所有屬於無線電類型的資產(見下文),但我正在努力尋找一種方法來僅返回無線電資產(最后一個頂點是在資產集合中)。
有沒有辦法從某個集合中獲取路徑中的最后一個頂點,有沒有更好的方法來執行此查詢?
FOR path
IN 1..100 OUTBOUND K_PATHS
'assets/493761' TO 'assetTypes/radio'
GRAPH assetRelationships
RETURN path
我已經設法使用用戶定義的 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;
};
我在設置數據庫連接時正在注冊
import { Database } from 'arangojs';
const database = new Database({
...
});
...
await database.createFunction('MY_FUNC::LAST_OF', lastOf.toString(), true);
然后在 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.