mirror of https://gitee.com/bigwinds/arangodb
587 lines
14 KiB
Plaintext
587 lines
14 KiB
Plaintext
!CHAPTER Graph Management
|
|
|
|
!SECTION Create a graph
|
|
|
|
<!-- @startDocuBlock JSF_general_graph_create -->
|
|
|
|
The creation of a graph requires the name of the graph and a definition of its edges.
|
|
|
|
For every type of edge definition a convenience method exists that can be used to create a graph.
|
|
|
|
```js
|
|
> var graph = require("org/arangodb/graph");
|
|
|
|
> var g = graph._create(graphName, edgeDefinitions);
|
|
```
|
|
|
|
There are different types of edge defintions:
|
|
|
|
!SUBSECTION Edge Definitions
|
|
|
|
<!-- @startDocuBlock @startDocuBlock JSF_general_graph_edgeDefinitions -->
|
|
|
|
|
|
To add further edge definitions to the array one must call:
|
|
|
|
```js
|
|
> graph._extendEdgeDefinitions(edgeDefinitions, edgeDefinition1,......edgeDefinitionN);
|
|
```
|
|
|
|
|
|
!SUBSUBSECTION Undirected Relation
|
|
|
|
`general-graph._undirectedRelationDefinition(relationName, vertexCollections)`
|
|
*Define an undirected relation.*
|
|
<br />
|
|
<br />
|
|
Defines an undirected relation with the name *relationName* using the
|
|
list of *vertexCollections*. This relation allows the user to store
|
|
edges in any direction between any pair of vertices within the
|
|
*vertexCollections*.
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
To define simple relation with only one vertex collection:
|
|
<br />
|
|
|
|
```
|
|
arangosh> var graph = require("org/arangodb/general-graph");
|
|
arangosh> graph._undirectedRelationDefinition("friend", "user");
|
|
{
|
|
"collection" : "friend",
|
|
"from" : [
|
|
"user"
|
|
],
|
|
"to" : [
|
|
"user"
|
|
]
|
|
}
|
|
```
|
|
<br />
|
|
To define a relation between several vertex collections:
|
|
<br />
|
|
|
|
```
|
|
arangosh> var graph = require("org/arangodb/general-graph");
|
|
arangosh> graph._undirectedRelationDefinition("marriage", ["female", "male"]);
|
|
{
|
|
"collection" : "marriage",
|
|
"from" : [
|
|
"female",
|
|
"male"
|
|
],
|
|
"to" : [
|
|
"female",
|
|
"male"
|
|
]
|
|
}
|
|
```
|
|
|
|
|
|
!SUBSUBSECTION Directed Relation
|
|
|
|
|
|
`general-graph._directedRelationDefinition(relationName, fromVertexCollections, toVertexCollections)`
|
|
*Define a directed relation.*
|
|
<br />
|
|
<br />
|
|
The *relationName* defines the name of this relation and references to the underlying edge collection.
|
|
The *fromVertexCollections* is an Array of document collections holding the start vertices.
|
|
The *toVertexCollections* is an Array of document collections holding the target vertices.
|
|
Relations are only allowed in the direction from any collection in *fromVertexCollections*
|
|
to any collection in *toVertexCollections*.
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var graph = require("org/arangodb/general-graph");
|
|
arangosh> graph._directedRelationDefinition("has_bought", ["Customer", "Company"], ["Groceries", "Electronics"]);
|
|
{
|
|
"collection" : "has_bought",
|
|
"from" : [
|
|
"Customer",
|
|
"Company"
|
|
],
|
|
"to" : [
|
|
"Groceries",
|
|
"Electronics"
|
|
]
|
|
}
|
|
```
|
|
|
|
|
|
|
|
!SUBSUBSECTION Complete Example to create a graph
|
|
|
|
Example Call:
|
|
|
|
```js
|
|
> var graph = require("org/arangodb/graph");
|
|
> var edgeDefinitions = graph._edgeDefinitions();
|
|
> graph._extendEdgeDefinitions(edgeDefinitions, graph._undirectedRelationDefinition("friend_of", ["Customer"]));
|
|
> graph._extendEdgeDefinitions(edgeDefinitions, graph._directedRelationDefinition("has_bought", ["Customer", "Company"], ["Groceries", "Electronics"]));
|
|
> graph._create("myStore", edgeDefinitions);
|
|
{
|
|
_id: "_graphs/123",
|
|
_rev: "123",
|
|
_key: "123"
|
|
}
|
|
```
|
|
|
|
alternative call:
|
|
|
|
```js
|
|
> var graph = require("org/arangodb/graph");
|
|
> var edgeDefinitions = graph._edgeDefinitions(graph._undirectedRelationDefinition("friend_of", ["Customer"]), graph._directedRelationDefinition("has_bought", ["Customer", "Company"], ["Groceries", "Electronics"]));
|
|
> graph._create("myStore", edgeDefinitions);
|
|
{
|
|
_id: "_graphs/123",
|
|
_rev: "123",
|
|
_key: "123"
|
|
};
|
|
```
|
|
!SUBSECTION Orphan Collections
|
|
|
|
Each graph has a list of orphan collections. It consists of arbitrary many vertex collections (type *document*), that are not
|
|
used in an edge definition of the graph. If the graph is extended with an edge definition using one of the orphans,
|
|
it will be removed from the orphan collections automatically.
|
|
|
|
!SUBSUBSECTION Add
|
|
|
|
|
|
Adds a vertex collection to the set of orphan collections of the graph. If the
|
|
collection does not exist, it will be created.
|
|
<br />
|
|
`general-graph._addOrphanCollection(orphanCollectionName, createCollection)`
|
|
<br />
|
|
*orphanCollectionName* - string : name of vertex collection.
|
|
*createCollection* - bool : if true the collection will be created if it does not exist. Default: true.
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var graph = require("org/arangodb/general-graph")
|
|
arangosh> var ed1 = graph._directedRelationDefinition("myEC1", ["myVC1"], ["myVC2"]);
|
|
arangosh> var g = graph._create("myGraph", [ed1, ed2]);
|
|
ReferenceError: ed2 is not defined
|
|
```
|
|
<br />
|
|
|
|
|
|
!SUBSUBSECTION Read
|
|
|
|
|
|
Returns all vertex collections of the graph, that are not used in an edge definition.
|
|
<br />
|
|
`general-graph._getOrphanCollections()`
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var graph = require("org/arangodb/general-graph")
|
|
arangosh> var ed1 = graph._directedRelationDefinition("myEC1", ["myVC1"], ["myVC2"]);
|
|
arangosh> var g = graph._create("myGraph", [ed1]);
|
|
[ArangoError 1925: graph already exists]
|
|
```
|
|
<br />
|
|
|
|
|
|
!SUBSUBSECTION Remove
|
|
|
|
|
|
Removes an orphan collection from the graph and deletes the collection, if it is not
|
|
used in any graph.
|
|
<br />
|
|
`general-graph._removeOrphanCollection()`
|
|
<br />
|
|
*orphanCollectionName* - string : name of vertex collection.
|
|
*dropCollection* - bool : if true the collection will be dropped if it is not used in any graph.
|
|
Default: true.
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var graph = require("org/arangodb/general-graph")
|
|
arangosh> var ed1 = graph._directedRelationDefinition("myEC1", ["myVC1"], ["myVC2"]);
|
|
arangosh> var g = graph._create("myGraph", [ed1]);
|
|
[ArangoError 1925: graph already exists]
|
|
```
|
|
<br />
|
|
|
|
!SUBSECTION List available graphs
|
|
|
|
|
|
<!-- @startDocuBlock JSF_general_graph_list_call -->
|
|
`general-graph._list()` *List all graphs.*
|
|
<br />
|
|
<br />
|
|
|
|
<!-- @startDocuBlock JSF_general_graph_list_info -->
|
|
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
<!-- @startDocuBlock JSF_general_graph_list_examples -->
|
|
```
|
|
arangosh> var graph = require("org/arangodb/general-graph");
|
|
arangosh> graph._list();
|
|
[
|
|
"social"
|
|
]
|
|
```
|
|
|
|
!SUBSECTION Load a graph
|
|
|
|
```js
|
|
> var graph = require("org/arangodb/graph");
|
|
|
|
> var g = graph._graph("myStore");
|
|
```
|
|
|
|
- - -
|
|
|
|
!SUBSECTION Remove a graph
|
|
|
|
|
|
Removes a graph from the collection *\_graphs*.
|
|
|
|
```js
|
|
> graph._drop(graphId, dropCollections);
|
|
true
|
|
```
|
|
|
|
graphId: string - id of the graph to be removed
|
|
dropCollections: bool - optional. *true* all collections of the graph will be deleted.
|
|
*false* no collection of the graph will be deleted. Default: *true*
|
|
|
|
|
|
|
|
!SECTION Vertex
|
|
|
|
!SUBSECTION Save
|
|
|
|
<!-- @startDocuBlock JSF_general_graph_vertex_collection_save -->
|
|
|
|
`general-graph.vertexCollectionName.save(data)` *Creates a new vertex*
|
|
<br />
|
|
<br />
|
|
Creates a new vertex in collection *vertexCollectionName*.
|
|
*data*: json - data of vertex
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.male.save({name: "Floyd", _key: "floyd"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "male/floyd",
|
|
"_rev" : "91260521",
|
|
"_key" : "floyd"
|
|
}
|
|
```
|
|
<br />
|
|
|
|
|
|
!SUBSECTION Replace
|
|
|
|
|
|
Replaces the data of a vertex in collection *vertexCollectionName*
|
|
<br />
|
|
`general-graph.vertexCollectionName.replace(vertexId, data, options)`
|
|
<br />
|
|
*vertexId*: string - id of the vertex
|
|
*data*: json - data of vertex
|
|
*options*: json - (optional) - see collection documentation
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.male.save({neym: "Jon", _key: "john"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "male/john",
|
|
"_rev" : "31360617",
|
|
"_key" : "john"
|
|
}
|
|
arangosh> g.male.replace("male/john", {name: "John"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "male/john",
|
|
"_rev" : "31557225",
|
|
"_key" : "john"
|
|
}
|
|
```
|
|
<br />
|
|
|
|
|
|
!SUBSECTION Update
|
|
|
|
|
|
Updates the data of a vertex in collection *vertexCollectionName*
|
|
<br />
|
|
`general-graph.vertexCollectionName.update(vertexId, data, options)`
|
|
<br />
|
|
*vertexId*: string - id of the vertex
|
|
*data*: json - data of vertex
|
|
*options*: json - (optional) - see collection documentation
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.female.save({name: "Lynda", _key: "linda"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "female/linda",
|
|
"_rev" : "79201897",
|
|
"_key" : "linda"
|
|
}
|
|
arangosh> g.female.update({name: "Linda", _key: "linda"});
|
|
TypeError: Object #<Object> has no method 'split'
|
|
```
|
|
<br />
|
|
|
|
|
|
!SUBSECTION Remove
|
|
|
|
|
|
Removes a vertex in collection *vertexCollectionName*
|
|
<br />
|
|
`general-graph.vertexCollectionName.remove(vertexId, options)`
|
|
<br />
|
|
Additionally removes all ingoing and outgoing edges of the vertex recursively
|
|
(see [edge remove](#edge.remove)).
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.male.save({name: "Kermit", _key: "kermit"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "male/kermit",
|
|
"_rev" : "83068521",
|
|
"_key" : "kermit"
|
|
}
|
|
arangosh> db._exists("male/kermit")
|
|
true
|
|
arangosh> g.male.remove("male/kermit")
|
|
true
|
|
arangosh> db._exists("male/kermit")
|
|
false
|
|
```
|
|
<br />
|
|
|
|
|
|
!SECTION Edge
|
|
|
|
!SUBSECTION Save
|
|
|
|
|
|
Creates and saves a new edge from vertex *from* to vertex *to* in
|
|
collection *edgeCollectionName*
|
|
<br />
|
|
`general-graph.edgeCollectionName.save(from, to, data)`
|
|
<br />
|
|
*from*: string - id of outgoing vertex
|
|
*to*: string - of ingoing vertex
|
|
*data*: json - data of edge
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.relation.save("male/bob", "female/alice", {type: "married", _key: "bobAndAlice"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "relation/bobAndAlice",
|
|
"_rev" : "45909609",
|
|
"_key" : "bobAndAlice"
|
|
}
|
|
```
|
|
<br />
|
|
If the collections of *from* and *to* are not defined in an edgeDefinition of the graph,
|
|
the edge will not be stored.
|
|
<br />
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.relation.save("relation/aliceAndBob", "female/alice", {type: "married", _key: "bobAndAlice"});
|
|
Edge is not allowed between relation/aliceAndBob and female/alice.
|
|
```
|
|
|
|
|
|
!SUBSECTION Replace
|
|
|
|
|
|
Replaces the data of an edge in collection *edgeCollectionName*
|
|
<br />
|
|
`general-graph.edgeCollectionName.replace(edgeId, data, options)`
|
|
<br />
|
|
*edgeId*: string - id of the edge
|
|
*data*: json - data of edge
|
|
*options*: json - (optional) - see collection documentation
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.relation.save("female/alice", "female/diana", {typo: "nose", _key: "aliceAndDiana"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "relation/aliceAndDiana",
|
|
"_rev" : "27362921",
|
|
"_key" : "aliceAndDiana"
|
|
}
|
|
arangosh> g.relation.replace("relation/aliceAndDiana", {type: "knows"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "relation/aliceAndDiana",
|
|
"_rev" : "27559529",
|
|
"_key" : "aliceAndDiana"
|
|
}
|
|
```
|
|
<br />
|
|
|
|
|
|
!SUBSECTION Update
|
|
|
|
|
|
Updates the data of an edge in collection *edgeCollectionName*
|
|
<br />
|
|
`general-graph.edgeCollectionName.update(edgeId, data, options)`
|
|
<br />
|
|
*edgeId*: string - id of the edge
|
|
*data*: json - data of edge
|
|
*options*: json - (optional) - see collection documentation
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.relation.save("female/alice", "female/diana", {type: "knows", _key: "aliceAndDiana"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "relation/aliceAndDiana",
|
|
"_rev" : "127108713",
|
|
"_key" : "aliceAndDiana"
|
|
}
|
|
arangosh> g.relation.update("relation/aliceAndDiana", {type: "quarrelled", _key: "aliceAndDiana"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "relation/aliceAndDiana",
|
|
"_rev" : "127305321",
|
|
"_key" : "aliceAndDiana"
|
|
}
|
|
```
|
|
<br />
|
|
|
|
|
|
!SUBSECTION Remove
|
|
|
|
|
|
Removes an edge in collection *edgeCollectionName*
|
|
<br />
|
|
`general-graph.edgeCollectionName.remove(edgeId, options)`
|
|
<br />
|
|
If this edge is used as a vertex by another edge, the other edge will be removed (recursively).
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g.relation.save("female/alice", "female/diana", {_key: "aliceAndDiana"});
|
|
{
|
|
"error" : false,
|
|
"_id" : "relation/aliceAndDiana",
|
|
"_rev" : "141657705",
|
|
"_key" : "aliceAndDiana"
|
|
}
|
|
arangosh> db._exists("relation/aliceAndDiana")
|
|
true
|
|
arangosh> g.relation.remove("relation/aliceAndDiana")
|
|
true
|
|
arangosh> db._exists("relation/aliceAndDiana")
|
|
false
|
|
```
|
|
<br />
|
|
|
|
|
|
!SECTION Vertices
|
|
|
|
!SUBSECTION Get vertex *from* of an edge
|
|
|
|
|
|
Get the vertex of an edge defined as *_from*
|
|
<br />
|
|
`general-graph._getFromVertex(edgeId)`
|
|
<br />
|
|
Returns the vertex defined with the attribute *_from* of the edge with *edgeId* as its *_id*.
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g._getFromVertex("relation/aliceAndBob")
|
|
{
|
|
"name" : "Alice",
|
|
"_id" : "female/alice",
|
|
"_rev" : "175670889",
|
|
"_key" : "alice"
|
|
}
|
|
```
|
|
<br />
|
|
|
|
|
|
!SUBSECTION Get vertex *to* of an edge
|
|
|
|
|
|
Get the vertex of an edge defined as *_to*
|
|
<br />
|
|
`general-graph._getToVertex(edgeId)`
|
|
<br />
|
|
Returns the vertex defined with the attribute *_to* of the edge with *edgeId* as its *_id*.
|
|
<br />
|
|
@EXAMPLES
|
|
<br />
|
|
|
|
```
|
|
arangosh> var examples = require("org/arangodb/graph-examples/example-graph.js");
|
|
arangosh> var g = examples.loadGraph("social");
|
|
arangosh> g._getToVertex("relation/aliceAndBob")
|
|
{
|
|
"name" : "Bob",
|
|
"_id" : "male/bob",
|
|
"_rev" : "40666729",
|
|
"_key" : "bob"
|
|
}
|
|
```
|
|
<br />
|