IT:ArangoDB:HowTo:API/JS/Graphs/Paths
Summary
We already touched on the very simple edge collection specific methods:
As well as stating that the PATH method offers a lot more options.
Process
Searching for Direct Relationships (Path Length of 1)
//Create a Statement to search for Paths
//that have a Start/Source of X,
//and a length of 1 (ie, direct friends of X)
//and return just the name
stmt = db._createStatement(
{
"query" : "FOR p IN PATHS(users, knows,'outbound')
FILTER p.source._id == @id
&&
LENGTH(p.edges) == 1
RETURN p.vertices[*].name"
});
stmt.bind("id", carla._id);
cursor = stmt.execute();
while (cursor.hasNext()) {
print(cursor.next());
}
Searching for Friends of Friends (Path Length of 2)
//create a statement for
//Paths with a start/source of X
//and length larger than 1 (ie not direct friends)
//and return just the name.
stmt = db._createStatement(
{
"query" : "FOR p IN PATHS(users, knows, 'outbound')
FILTER p.source._id == @id
&& LENGTH(p.edges) 2
RETURN p.vertices[*].name"
});
stmt.bind("id", carla._id);
cursor = stmt.execute();
while (cursor.hasNext()) {
print(cursor.next());
}
Searching for all InDirect Relationships (Path Length of >1)
//create a statement for
//Paths with a start/source of X
//and length larger than 1 (ie not direct friends)
//and return just the name.
stmt = db._createStatement(
{
"query" : "FOR p IN PATHS(users, edges, 'outbound')
FILTER p.source._id == @id
&& LENGTH(p.edges) > 1
&& LENGTH(p.edges) < 7
RETURN p.vertices[*].name"
});
stmt.bind("id", carla._id);
cursor = stmt.execute();
while (cursor.hasNext()) {
print(cursor.next());
}
Who knows X
//Changing 'outbound' to 'inbound'
//create a statement for Paths
//with source/start of X, and length 1
//returning the terminal vertex (the second vertex...)
stmt = db._createStatement(
{ "query" : "FOR p IN PATHS(users, knows, 'inbound')
FILTER p.source._id == @id
&& LENGTH(p.edges) == 1
RETURN p.vertices[1].name" });
stmt.bind("id", carla._id);
cursor = stmt.execute();
while (cursor.hasNext()) {
print(cursor.next());
}