讓我從arangosh開始,因爲它更易於閱讀。我將給出HTTP命令作爲附錄。
ArangoDB殼牌
您需要三個文件集合「艦隊」,「雙雄」,和你上面所說的「飛行員」和至少一個邊緣 收集保存圖形結構。如果你想遍歷在「擁有」和「inFleet」和「can」「之間跳躍的圖形結構,我建議 使用一個集合」關係「並給邊緣一個屬性」type「。
另一種解決方案是使用三個邊緣集合「擁有」和「inFleet」和「can」「。爲了提出更基於事實的建議,最好多瞭解一下你的用例。
arangosh [_system]> db._create("fleets");
[ArangoCollection 139792431, "fleets" (type document, status loaded)]
arangosh [_system]> db._create("planes");
[ArangoCollection 140382255, "planes" (type document, status loaded)]
arangosh [_system]> db._create("pilots");
[ArangoCollection 140972079, "pilots" (type document, status loaded)]
arangosh [_system]> db._createEdgeCollection("relations");
[ArangoCollection 141103151, "relations" (type edge, status loaded)]
接下來,在集合「車隊」中創建文檔。我將使用航空公司的名稱作爲關鍵。根據您的使用情況,可能會有更好的密鑰。例如,也許有一個通用的縮寫(如漢莎航空LH)。
arangosh [_system]> db.fleets.save({ _key: "airline0", name: "Airline 0" });
arangosh [_system]> db.fleets.save({ _key: "airline1", name: "Airline 1" });
重複的戰機和飛行員一樣:
arangosh [_system]> db.planes.save({ _key: "plane0", name: "Plane Zero", color: "red" })
arangosh [_system]> db.planes.save({ _key: "plane1", name: "Plane One", color: "red" })
arangosh [_system]> db.planes.save({ _key: "plane2", name: "Plane One", color: "green" })
arangosh [_system]> db.pilots.save({ _key: "jennifer", name: "Jenifer" });
您應該根據自己的使用情況選擇鍵。如果沒有「自然」鍵,則省略「_key」屬性。 ArangoDB將爲您生成一個唯一的密鑰。
接下來,添加上面創建的節點之間的關係。 ArangoDB 2.8中的語法與上面創建的文檔類似。另外, 您需要提供要連接的頂點的「from」和「to」鍵。
arangosh [_system]> db.relations.save("fleets/airline0", "planes/plane0", { type: 'owns' });
arangosh [_system]> db.relations.save("fleets/airline0", "planes/plane1", { type: 'owns', since: 2013 });
arangosh [_system]> db.relations.save("fleets/airline1", "planes/plane2", { type: 'owns' });
arangosh [_system]> db.relations.save("fleets/airline1", "planes/plane1", { type: 'previouslyOwned', begin: 1999, end: 2013 });
arangosh [_system]> db.relations.save("pilots/jennifer", "planes/plane0", { type: 'canfly' });
如果「inFleet」 /「wasInFleet」 /「hasPilot」是的反向「擁有」 /「previouslyOwned」 /「canfly」,比你並不需要創建一個單獨的邊緣了,因爲邊緣是定向的。
如果有「擁有」和「inFleet」你可以創建類似上述的關係之間的差異:
arangosh [_system]> db.relations.save("planes/plane0", "fleets/airline0", { type: 'inFleet' });
...
現在,爲了遵循路徑「詹妮弗能飛PLANEX通過airlineY擁有」使用:
arangosh> db._query("FOR v, e IN OUTBOUND 'pilots/jennifer' relations FILTER e.type == 'canfly' FOR w, f IN INBOUND v relations FILTER f.type == 'owns' RETURN { plane: v, airline: w }")
[
{
"plane" : {
"color" : "red",
"name" : "Plane Zero",
"_id" : "planes/plane0",
"_rev" : "153686063",
"_key" : "plane0"
},
"airline" : {
"name" : "Airline 0",
"_id" : "fleets/airline0",
"_rev" : "149884975",
"_key" : "airline0"
}
}
]
或者扭轉路徑(不使用 'inFleet' 和 'hasPilot'):
arangosh> db._query("FOR v, e IN OUTBOUND 'fleets/airline0' relations FILTER e.type == 'owns' FOR w, f IN INBOUND v relations FILTER f.type == 'canfly' RETURN { plane: v, w: w }")
[
{
"plane" : {
"color" : "red",
"name" : "Plane Zero",
"_id" : "planes/plane0",
"_rev" : "153686063",
"_key" : "plane0"
},
"w" : {
"_id" : "pilots/jennifer",
"_rev" : "330240047",
"_key" : "jennifer"
}
}
]
HTTP
讓我舉例說明上面執行的各種類型的命令。
arangosh [_system]> db._create("fleets");
這轉化爲
curl -X POST --data-binary @- --dump - http://localhost:8529/_api/collection <<EOF
{
"name" : "fleets"
}
EOF
接着
arangosh [_system]> db._createEdgeCollection("relations");
轉化爲
curl -X POST --data-binary @- --dump - http://localhost:8529/_api/collection <<EOF
{
"name" : "relations", "type": 3
}
EOF
接着
arangosh [_system]> db.fleets.save({ _key: "airline0", name: "Airline 0" });
轉化爲
curl -X POST --data-binary @- --dump - http://localhost:8529/_api/document?collection=products <<EOF
{ "_key": "airline0", "name": "Airline 0" }
EOF
下一頁
db.relations.save("pilots/jennifer", "planes/plane0", { type: 'canfly' });
轉化爲
curl -X POST --data-binary @- --dump - http://localhost:8529/_api/edge/?collection=relations&from=pilots/jennifer&to=planes/plane0 <<EOF
{
"type" : "canfly"
}
EOF
完美!謝謝!在深入研究文檔後,我發現了其中的一些內容,但直到現在,我還不清楚這種遍歷。我很想了解更多有關單個邊緣集合與多個集合的優勢。我可以遍歷多個邊緣集合嗎?或者我只能在一個內部遍歷? –
另一個問題:'graph._addVertexCollection'和'db._create'有什麼區別? –
ArangoDB支持有名和無名圖形。命名圖形用「圖形...」創建。您還可以使用使用「db ...」創建的邊緣集合的匿名圖形。是的,你可以使用多個邊緣集合來遍歷。由於評論的大小受到限制,因此可以通過https://groups.google.com/forum/#!forum/arangodb繼續進行討論。 – fceller