In this part, we will consider a couple of AQL Example Queries on an Actors and Movies Database. These inquiries depend on charts.
Issue
Given an assortment of entertainers and an assortment of films, and an actIn edges assortment (with a year property) to associate the vertex as shown underneath −
[Actor] <-act in - > [Movie]
How would we get −
- All entertainers who acted in "movie1" OR "movie2"?
- All entertainers who acted in both "movie1" AND "movie2"?
- All normal films somewhere in the range of "actor1" and "actor2"?
- All entertainers who acted in at least 3 films?
- All motion pictures where precisely 6 entertainers acted in?
- The quantity of entertainers by film?
- The quantity of motion pictures by entertainer?
- The quantity of motion pictures acted in the middle of 2005 and 2010 by entertainer?
Arrangement
During the way toward settling and acquiring the responses to the above questions, we will utilize Arangosh to make the dataset and run inquiries on that. All the AQL questions are strings and can just be duplicated over to your number one driver rather than Arangosh.
# wget -O dataset.js
https://drive.google.com/file/d/0B4WLtBDZu_QWMWZYZ3pYMEdqajA/view?usp=sharing
Yield
...
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘dataset.js’
dataset.js [ <=> ] 115.14K --.-KB/s in 0.01s
2017-09-17 14:19:12 (11.1 MB/s) - ‘dataset.js’ saved [117907]
You can find in the yield over that we have downloaded a JavaScript record dataset.js. This record contains the Arangosh orders to make the dataset in the information base. Rather than reordering the orders individually, we will utilize the - javascript.execute choice on Arangosh to execute the different orders non-intelligently. Think of it as the lifeline order!
Presently execute the accompanying order on the shell −
$ arangosh --javascript.execute dataset.js
Supply the secret word when incited as you can find in the above screen capture. Presently we have saved the information, so we will develop the AQL inquiries to address the particular inquiries brought up in the start of this section.
First Question
Allow us to take the primary inquiry: All entertainers who acted in "movie1" OR "movie2". Assume, we need to discover the names of the relative multitude of entertainers who acted in "TheMatrix" OR "TheDevilsAdvocate" −
We will begin with each film in turn to get the names of the entertainers −
127.0.0.1:8529@_system> db._query("FOR x IN ANY 'movies/TheMatrix' actsIn
OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN x._id").toArray();
Yield
We will get the accompanying yield −
[
"actors/Hugo",
"actors/Emil",
"actors/Carrie",
"actors/Keanu",
"actors/Laurence"
]
Presently we keep on shaping a UNION_DISTINCT of two NEIGHBORS questions which will be the arrangement −
127.0.0.1:8529@_system> db._query("FOR x IN UNION_DISTINCT ((FOR y IN ANY
'movies/TheMatrix' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN
y._id), (FOR y IN ANY 'movies/TheDevilsAdvocate' actsIn OPTIONS {bfs: true,
uniqueVertices: 'global'} RETURN y._id)) RETURN x").toArray();
Yield
[
"actors/Charlize",
"actors/Al",
"actors/Laurence",
"actors/Keanu",
"actors/Carrie",
"actors/Emil",
"actors/Hugo"
]
Second Question
Allow us presently to think about the subsequent inquiry: All entertainers who acted in both "movie1" AND "movie2". This is practically indistinguishable from the inquiry above. However, this time we are not inspired by a UNION yet in an INTERSECTION −
127.0.0.1:8529@_system> db._query("FOR x IN INTERSECTION ((FOR y IN ANY
'movies/TheMatrix' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN
y._id), (FOR y IN ANY 'movies/TheDevilsAdvocate' actsIn OPTIONS {bfs: true,
uniqueVertices: 'global'} RETURN y._id)) RETURN x").toArray();
Yield
We will get the accompanying yield −
[
"actors/Keanu"
]
Third Question
Allow us presently to think about the third inquiry: All basic films somewhere in the range of "actor1" and "actor2". This is really indistinguishable from the inquiry concerning regular entertainers in movie1 and movie2. We simply need to change the beginning vertices. For instance, let us locate all the motion pictures where Hugo Weaving ("Hugo") and Keanu Reeves are co-featuring −
127.0.0.1:8529@_system> db._query(
"FOR x IN INTERSECTION (
(
FOR y IN ANY 'actors/Hugo' actsIn OPTIONS
{bfs: true, uniqueVertices: 'global'}
RETURN y._id
),
(
FOR y IN ANY 'actors/Keanu' actsIn OPTIONS
{bfs: true, uniqueVertices:'global'} RETURN y._id
)
)
RETURN x").toArray();
Yield
We will get the accompanying yield −
[
"movies/TheMatrixReloaded",
"movies/TheMatrixRevolutions",
"movies/TheMatrix"
]
Fourth Question
Allow us currently to think about the fourth inquiry. All entertainers who acted in at least 3 motion pictures. This inquiry is unique; we can't utilize the neighbors work here. Rather we will utilize the edge-file and the COLLECT assertion of AQL for gathering. The essential thought is to gather all edges by their startVertex (which in this dataset is consistently the entertainer). At that point we eliminate all entertainers with under 3 films from the outcome as here we have incorporated the quantity of motion pictures an entertainer has acted in −
127.0.0.1:8529@_system> db._query("FOR x IN actsIn COLLECT actor = x._from WITH
COUNT INTO counter FILTER counter >= 3 RETURN {actor: actor, movies:
counter}"). toArray()
Yield
[
{
"actor" : "actors/Carrie",
"movies" : 3
},
{
"actor" : "actors/CubaG",
"movies" : 4
},
{
"actor" : "actors/Hugo",
"movies" : 3
},
{
"actor" : "actors/Keanu",
"movies" : 4
},
{
"actor" : "actors/Laurence",
"movies" : 3
},
{
"actor" : "actors/MegR",
"movies" : 5
},
{
"actor" : "actors/TomC",
"movies" : 3
},
{
"actor" : "actors/TomH",
"movies" : 3
}
]
For the excess inquiries, we will examine the inquiry arrangement, and give the questions as it were. The peruser should run the actual inquiry on the Arangosh terminal.
Fifth Question
Allow us currently to think about the fifth inquiry: All motion pictures where precisely 6 entertainers acted in. A similar thought as in the inquiry previously, yet with the equity channel. Be that as it may, presently we need the film rather than the entertainer, so we return the _to quality −
db._query("FOR x IN actsIn COLLECT movie = x._to WITH COUNT INTO counter FILTER
counter == 6 RETURN movie").toArray()
The quantity of entertainers by film?
We recollect in our dataset _to on the edge relates to the film, so we tally how regularly the equivalent _to shows up. This is the quantity of entertainers. The inquiry is practically indistinguishable from the ones preceding yet without the FILTER after COLLECT −
db._query("FOR x IN actsIn COLLECT movie = x._to WITH COUNT INTO counter RETURN
{movie: movie, actors: counter}").toArray()
Sixth Question
Allow us presently to think about the 6th inquiry: The quantity of motion pictures by an entertainer.
The manner in which we discovered answers for our above inquiries will help you discover the answer for this inquiry too.
db._query("FOR x IN actsIn COLLECT actor = x._from WITH COUNT INTO counter
RETURN {actor: actor, movies: counter}").toArray()