Get query
In this guide, you’ll see how to retrieve data from a database using Studio, Console, or one of the drivers.
Understanding Get queries
A Get query is used to retrieve concepts from a TypeDB database. For a detailed explanation of the query see the Get query page.
Sending Get queries
Before you get started, ensure you followed instructions from the Writing data section. The easiest way to send a Get query is to use Studio or Console:
-
Studio
-
Console
Follow the Studio manual to connect to TypeDB and select a project folder. Then use the steps below:
-
Use a drop-down list in the top toolbar to select a database.
-
Switch to
data
session andread
transaction types. -
Open a new tab and insert or type in an Insert query, for example:
TypeQL Get querymatch $u isa user, has email $e; get $u, $e;
-
Run the query by clicking the Run Query button.
-
Open a
data
session andread
transaction to the selected database (e.g.,sample_db
):transaction sample_db data read
-
Send the Get query:
match $u isa user, has email $e; get $u, $e;
Push Enter twice to send the query.
-
Close the transaction:
close
The resulted Log output should look like this:
{
$e alice@typedb.com isa email;
$u iid 0x826e80018000000000000000 isa user;
}
{
$e the-bob@typedb.com isa email;
$u iid 0x826e80018000000000000001 isa user;
}
{
$e charlie@typedb.com isa email;
$u iid 0x826e80018000000000000002 isa user;
}
To send a Get query programmatically, use drivers:
-
Rust
-
Python
-
Node.js
-
Java
-
C#
-
C++
-
C
Follow the connection guide
to connect the driver
to TypeDB Cloud or TypeDB Core.
let databases = DatabaseManager::new(driver);
let db = databases.get(DB_NAME)?;
{
let session = Session::new(db, SessionType::Data)?;
{
let tx = session.transaction(TransactionType::Read)?;
let get_query = "
match
$u isa user, has email $e;
get
$e;
";
let response = tx.query().get(get_query)?;
for (i, cm) in response.enumerate() {
let email_concept = cm.unwrap().get("e").unwrap().clone();
let email = match email_concept {
Concept::Attribute(Attribute { value: Value::String(value), .. }) => value,
_ => unreachable!(),
};
println!("Email #{}: {}", (i + 1).to_string(), email)
}
}
}
Follow the connection guide
to connect the driver
to TypeDB Cloud or TypeDB Core.
with driver.session(DB_NAME, SessionType.DATA) as session:
with session.transaction(TransactionType.READ) as tx:
get_query = """
match
$u isa user, has email $e;
get
$e;
"""
response = tx.query.get(get_query)
for i, concept_map in enumerate(response, start=1):
email = concept_map.get("e").as_attribute().get_value()
print(f"Email #{i}: {email}")
Follow the connection guide
to connect the driver
to TypeDB Cloud or TypeDB Core.
try {
session = await driver.session(DB_NAME, SessionType.DATA);
try {
tx = await session.transaction(TransactionType.READ);
const get_query = `
match
$u isa user, has email $e;
get
$e;
`;
let response = await tx.query.get(get_query).collect();
for (let i = 0; i < response.length; i++) {
console.log("Email #" + (i + 1) + ": " + response[i].get("e").value);
}
}
finally {if (tx.isOpen()) {await tx.close()};}
}
finally {await session?.close();}
Follow the connection guide
to connect the driver
to TypeDB Cloud or TypeDB Core.
try (TypeDBSession session = driver.session(DB_NAME, TypeDBSession.Type.DATA)) {
try (TypeDBTransaction tx = session.transaction(TypeDBTransaction.Type.READ)) {
String getQuery = """
match
$u isa user, has email $e;
get
$e;
""";
int[] ctr = new int[1];
tx.query().get(getQuery).forEach(result -> System.out.println("Email #" + (++ctr[0]) + ": " + result.get("e").asAttribute().getValue().toString()));
}
}
Follow the connection guide
to connect the driver
to TypeDB Cloud or TypeDB Core.
using (ITypeDBSession session = driver.Session(DB_NAME, SessionType.Data)) {
using (ITypeDBTransaction tx = session.Transaction(TransactionType.Read)) {
string get_query = @"
match
$u isa user, has email $e;
get
$e;";
List<IConceptMap> response = tx.Query.Get(get_query).ToList();
foreach (IConceptMap answer in response) {
Console.WriteLine("Email: " + answer.Get("e").AsAttribute().Value);
}
}
}
Follow the connection guide
to connect the driver
to TypeDB Cloud or TypeDB Core.
TypeDB::Options options;
TypeDB::Session session = driver.session(DB_NAME, TypeDB::SessionType::DATA, options);
{
TypeDB::Transaction tx = session.transaction(TypeDB::TransactionType::READ, options);
std::string getQuery = R"(
match
$u isa user, has email $e;
get
$e;
)";
TypeDB::ConceptMapIterable result = tx.query.get(getQuery);
int16_t i = 0;
for (TypeDB::ConceptMap& cm : result) {
i += 1;
std::cout << "Email #" << std::to_string(i) << ": " << cm.get("e")->asAttribute()->getValue()->asString() << std::endl;
}
}
Follow the connection guide
to connect the driver
to TypeDB Cloud or TypeDB Core.
Options* opts = options_new();
session = session_new(databaseManager, DB_NAME, Data, opts);
tx = transaction_new(session, Read, opts);
if ((tx == NULL) || FAILED()) {
handle_error("Transaction failed to start.");
goto cleanup;
}
char query[512];
snprintf(query, sizeof(query), "match $u isa user, has email $e; get $e;");
ConceptMapIterator* response = query_get(tx, query, opts);
if (FAILED()) {
handle_error("Query execution failed.");
goto cleanup;
}
ConceptMap* CM;
while ((CM = concept_map_iterator_next(response)) != NULL) {
Concept* emailConcept = concept_map_get(CM, "e");
printf("%s %s\n", "User:", value_get_string(attribute_get_value(emailConcept)));
}
transaction_close(tx);
session_close(session);
Response interpretation
A Get query returns all concept that were matched by the match
clause and filtered by the get
clause.
The response is a Stream/Iterator with a ConceptMap object for every solution of a match
clause pattern.
A concept is returned as a special object with methods relevant to its type.
There are modifiers that can be applied to a Get query that can change its output format:
-
Grouping returns a Stream/Iterator of ConceptMapGroup objects.
-
Aggregation returns Promise of a Value.
-
Grouping with aggregation returns a Stream/Iterator of ValueGroup object.
See the respective drivers API reference for relevant methods to work with these objects: