![](/img/trans.png)
[英]In ArangoDB AQL, how to return both vertices and edges from a graph traversal?
[英]Returning a single document of vertices and edges from an AQL traversal
當我在Arango中進行遍歷時,我得到一個json結構數組,如下所示:
{
"vertex" : {
"_id" : "vertices/857831247835",
"_key" : "857831247835",
"_rev" : "857831247835",
},
"path" : {
"edges" : [
{
"_id" : "edges/857831575515",
"_key" : "857831575515",
"_rev" : "857831575515",
"_from" : "vertices/857831247835",
"_to" : "vertices/857821417435",
}
],
"vertices" : [
{
"_id" : "vertices/857821417435",
"_key" : "857821417435",
"_rev" : "857821417435",
},
{
"_id" : "vertices/857831247835",
"_key" : "857831247835",
"_rev" : "857831247835",
}
]
},
"startVertex" : "vertices/857821417435"
}
有沒有辦法將遍歷中找到的所有邊/頂點變為單個結構,就像上面使用AQL一樣?
您實際上可以使用兩種方法來獲得結果:
輕松擴展您的AQL查詢:
FOR x IN (<<Your previous query here>>) RETURN {edges: x.path.edges, vertices: x.path.vertices, vertex: x.vertex, startVertex: x.startVertex}
更arangosh
方式(短路對象創建):注冊以下用戶定義的函數: https : arangosh
一次使用arangosh
。
這應該是這樣的:
require("org/arangodb/aql/functions").register("myVisitors::flatVisitor", function (config, result, vertex, path) { result.push({ vertex: vertex, edges: path.edges, vertices: path.vertices}); });
然后在你的AQL中添加額外的選項visitor: "myVisitors::flatVisitor"
與paths: true
相同。
BTW:在這種情況下, paths: true
將被忽略,因為它僅在我們的默認訪問者中使用。
提示:如果您只需要結果中的某些屬性而不是完整文檔,則只需在訪問者中返回這些屬性。 這將顯着提高性能。
有點舊,但為了這個問題的新訪問者。 另一種方法是從路徑中唯一地累積邊緣(語法是arangojs官方客戶端):
graph.traversal('verticies/startkey', {
direction: 'any',
init: `result.verticies = [];
result.edges = [];`,
visitor: `
path.edges
.forEach(function (x) {
if (result.edges.indexOf(x) === -1) {
result.edges.push(x);
}
});
result.verticies.push(vertex);
`,
uniqueness: {
"vertices": "global",
"edges": "global"
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.