Reading data
There is only one query type that can read data from a TypeDB database: Get query.
All examples provided on this page are designed for the IAM database schema and data. To set the IAM database follow the Quickstart guide. |
Get query
A Get query is used to retrieve data from a TypeDB database.
Syntax
Get queries are written in TypeQL with the following syntax:
match <pattern>
[get <variable> [(, <variable>)...];]
[sort <variable> [asc|desc];]
[offset <value>;]
[limit <value>;]
[group <variable>;]
[count;] | [sum|max|min|mean|median|std <variable>;]
Examples
A simple example:
match
$p isa person, has full-name "Kevin Morrison", has email $e;
get $e;
This example query contains the following variables:
-
$p
, -
$e
.
The full-name
attribute is not bound to a variable in this query.
The example above matches person ($p
) that has a full-name
attribute value of Kevin Morrison
, and has email
attribute ($e
). The get
clause then filters the results to receive only the email
($e
) attributes.
A more complex example:
match
$pe ($x, $y) isa permission;
$x isa person, has full-name $x-n;
$x-n contains "Kevin";
$y (object: $o, action: $act) isa access;
$act has name $act-n;
$o has path $o-fp;
get $x-n, $act-n, $o-fp;
The example above uses a match
clause to do the following:
-
Find
permission
relation ($pe
), connecting$x
and$y
variables. -
Find
$x
as instance ofperson
entity type that has thefull-name
attribute with the value of$x-n
. -
$x-n
should contain stringKevin
. -
Find
access
relation of$o
asobject
role and$act
asaction
role. -
$act
should have thename
attribute with the value of$act-n
. -
$o
should have thepath
attribute with the value of$o-fp
.
The get
clause then filters the answers from the match
clause to retrieve only the following:
-
$x-n
, -
$act-n
, -
$o-fp
.
Every returned result should contain all three concepts.
An example with modifiers:
match
$pe ($x, $y) isa permission;
$x isa person, has full-name $x-n;
$y (object: $o, action: $act) isa access;
$act has name $act-n;
$o has path $o-fp;
get $x-n, $act-n, $o-fp;
sort $o-fp asc;
limit 3;
group $o-fp;
The get
clause filters the answers from the match
clause to retrieve only the following:
-
$x-n
, -
$act-n
, -
$o-fp
.
Then we sort the results by the $o-fp
in ascending order, limit the number of results to the first three, and
group the results by the $o-fp
variable values.
See example of response to such a query
The following or similar result can be obtained by running the query above without inference on the TypeDB server with the IAM schema and dataset from the Quickstart guide.
"LICENSE" isa path => {
{
$act-n "modify_file" isa name;
$x-n "Pearle Goodman" isa full-name;
$o-fp "LICENSE" isa path;
} {
$act-n "modify_file" isa name;
$x-n "Kevin Morrison" isa full-name;
$o-fp "LICENSE" isa path;
}
}
"README.md" isa path => {
{
$act-n "modify_file" isa name;
$x-n "Pearle Goodman" isa full-name;
$o-fp "README.md" isa path;
}
}