Thursday, 2 September 2021

ArangoDB: Working with edge documents

Edge document are like normal document with additional two attributes _from, _to.

 

Edge collection has ‘edges’ method which takes a vertex (or) vertices as input and return all edges starting from (outbound) or ending in (inbound) vertex (or) vertices.

 

Signature

edge-collection.edges(vertex)

edge-collection.edges(vertices)

 

Let’s experiment it with by building simple graph.


Step 1: Create two collections, one is of type document/vertex and other is of type edge.

 

db._createDocumentCollection("myVertexes")

db._createEdgeCollection("myEdges")

127.0.0.1:8529@abc_org> db._createDocumentCollection("myVertexes")
[ArangoCollection 36584, "myVertexes" (type document, status loaded)]

127.0.0.1:8529@abc_org> db._createEdgeCollection("myEdges")
[ArangoCollection 36601, "myEdges" (type edge, status loaded)]

 

Step 2: Create 5 vertex documents v1, v2, v3, v4 and v5.

var myGraph = {};
myGraph.v1 = db.myVertexes.save({ name : "vertex 1" });
myGraph.v2 = db.myVertexes.save({ name : "vertex 2" });
myGraph.v3 = db.myVertexes.save({ name : "vertex 3" });
myGraph.v4 = db.myVertexes.save({ name : "vertex 4" });
myGraph.v5 = db.myVertexes.save({ name : "vertex 5" });

127.0.0.1:8529@abc_org> var myGraph = {};

127.0.0.1:8529@abc_org> myGraph.v1 = db.myVertexes.save({ name : "vertex 1" });
{ 
  "_id" : "myVertexes/36641", 
  "_key" : "36641", 
  "_rev" : "_cS_C0bq---" 
}


 

127.0.0.1:8529@abc_org> myGraph.v2 = db.myVertexes.save({ name : "vertex 2" });
{ 
  "_id" : "myVertexes/36643", 
  "_key" : "36643", 
  "_rev" : "_cS_C0bq--A" 
}

127.0.0.1:8529@abc_org> myGraph.v3 = db.myVertexes.save({ name : "vertex 3" });
{ 
  "_id" : "myVertexes/36645", 
  "_key" : "36645", 
  "_rev" : "_cS_C0bu---" 
}

127.0.0.1:8529@abc_org> myGraph.v4 = db.myVertexes.save({ name : "vertex 4" });
{ 
  "_id" : "myVertexes/36647", 
  "_key" : "36647", 
  "_rev" : "_cS_C0by---" 
}

127.0.0.1:8529@abc_org> myGraph.v5 = db.myVertexes.save({ name : "vertex 5" });
{ 
  "_id" : "myVertexes/36649", 
  "_key" : "36649", 
  "_rev" : "_cS_C02u---" 
}

 

Step 3: Create edges between vertexes.

myGraph.e1 = db.myEdges.save(myGraph.v1, myGraph.v2,{ label : "linkTo"});
myGraph.e2 = db.myEdges.save(myGraph.v1, myGraph.v3,{ label : "linkTo"});
myGraph.e3 = db.myEdges.save(myGraph.v2, myGraph.v4,{ label : "linkTo"});
myGraph.e4 = db.myEdges.save(myGraph.v3, myGraph.v5,{ label : "linkTo"});
myGraph.e5 = db.myEdges.save(myGraph.v4, myGraph.v3,{ label : "linkTo"});
myGraph.e6 = db.myEdges.save(myGraph.v4, myGraph.v5,{ label : "linkTo"});

 

Get all the inbound and outbound edges of vertex v3

127.0.0.1:8529@abc_org> db.myEdges.edges(myGraph.v3)
[ 
  { 
    "_key" : "36717", 
    "_id" : "myEdges/36717", 
    "_from" : "myVertexes/36645", 
    "_to" : "myVertexes/36649", 
    "_rev" : "_cS_EWBq---", 
    "label" : "linkTo" 
  }, 
  { 
    "_key" : "36713", 
    "_id" : "myEdges/36713", 
    "_from" : "myVertexes/36641", 
    "_to" : "myVertexes/36645", 
    "_rev" : "_cS_EWBi---", 
    "label" : "linkTo" 
  }, 
  { 
    "_key" : "36719", 
    "_id" : "myEdges/36719", 
    "_from" : "myVertexes/36647", 
    "_to" : "myVertexes/36645", 
    "_rev" : "_cS_EWBu---", 
    "label" : "linkTo" 
  } 
]

 

Get all outbound edges of vertex v3

127.0.0.1:8529@abc_org> db.myEdges.outEdges(myGraph.v3)
[ 
  { 
    "_key" : "36717", 
    "_id" : "myEdges/36717", 
    "_from" : "myVertexes/36645", 
    "_to" : "myVertexes/36649", 
    "_rev" : "_cS_EWBq---", 
    "label" : "linkTo" 
  } 
]

 

Get all inbound edges of vertex v3

127.0.0.1:8529@abc_org> db.myEdges.inEdges(myGraph.v3)
[ 
  { 
    "_key" : "36713", 
    "_id" : "myEdges/36713", 
    "_from" : "myVertexes/36641", 
    "_to" : "myVertexes/36645", 
    "_rev" : "_cS_EWBi---", 
    "label" : "linkTo" 
  }, 
  { 
    "_key" : "36719", 
    "_id" : "myEdges/36719", 
    "_from" : "myVertexes/36647", 
    "_to" : "myVertexes/36645", 
    "_rev" : "_cS_EWBu---", 
    "label" : "linkTo" 
  } 
]

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment