让我们从arangosh开始,因为它更容易阅读。我将把HTTP命令作为补充说明。
ArangoDB Shell
如您所提到的,您需要三个文档集合“fleets”、“planes”和“pilots”,以及至少一个边缘集合来保存图形结构。如果您想在“拥有”、“inFleet”和“canfly”之间跳转遍历图形结构,则建议使用一个名为“relations”的集合,并给边缘分配一个属性“type”。
另一种解决方案是使用三个边缘集合“owns”、“inFleet”和“canfly”。为了提供更基于事实的推荐,了解更多关于您的用例将会很有帮助。
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)]
接下来,在“fleets”集合中创建文档。我将使用航空公司名称作为键。根据您的用例,可能有更好的键。例如,可能存在通用缩写(如Lufthansa的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'和'owns'/'previouslyOwned'/'canfly'相反,则不需要为它创建单独的边,因为边是有方向性的。
如果'owns'和'inFleet'之间存在差异,您可以按照上述方式创建关系:
arangosh [_system]> db.relations.save("planes/plane0", "fleets/airline0", { type: 'inFleet' });
...
现在,为了跟随“Jennifer可以驾驶属于AirlineY的PlaneX”的路径,请使用以下命令:
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
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
之间的区别是什么? - Nate Gardner