An alias is a unique name (identifier) assigned to represent a collection of data of a specific type. Aliases allow users to reference these data throughout the query, enabling data retrieval, manipulation, and further operations.
Declaration and Reference
An alias can be declared using the keyword as
immediately after some statements (known as a statement alias) or within a statement method (known as a method alias). The alias represents the data retrieved or generated by the statement.
You can reference an alias later in the query to further utilize the data or return it to the client.
Examples
Refer to the syntax of each statement to determine whether the statement alias and method aliases are supported.
To declare alias e
for the find().edges()
statement and reference it in the RETURN
statement:
find().edges({@direct}) as e
return e.time
The following query throws syntax error because declaring alias for the method edges()
is not allowed:
find().edges({@direct} as e)
return e
To declare alias paths
for the autonet()
statement and alias start
for its method src()
, then reference them in the RETURN
statement:
autonet().src({age < 60} as start).dest({@event}).depth(3) as paths
return start, paths
To declare alias a
for the find().nodes()
statement, reference it in the WITH
statement to compute the minimum age as minAge
, reference minAge
in another find().nodes()
statement to retrieve accounts with that age as alias b
, and finally reference b
in the RETURN
statement to output their names:
find().nodes({@account}) as a
with min(a.age) as minAge
find().nodes({@account.age == minAge}) as b
return b.name
In a path template statement, declare alias start
in the first n()
and reference it in the last n()
to form a simple cycle:
n(as start).e()[3].n({_id == start._id}) as p
return p limit 1
Naming Conventions
Naming conventions for aliases are:
- 1 to 64 characters.
- Cannot start with a tilde (
~
). - Cannot contain backquotes (
`
) or any reserved keywords.
It is suggested not to use a property name for an alias. If necessary, use the system alias this to disambiguate when required.
Alias Type
The alias type matches the data it represents.
In this query, the alias users
represents nodes and is of the type NODE
, while the alias ages
represents the values of the property age
, sharing the same type as age
:
find().nodes({@user}) as users
return users.age as ages
Referencing an Alias
Depending on the alias type, you can either reference the alias directly in certain statements or extract specific data from the alias for use.
The table below shows examples of alias referencing with different alias types:
Alias (Alias Type) |
Alias Reference | Data Represented | Data Type |
---|---|---|---|
n ( NODE ) |
n |
Nodes | NODE |
n.name |
Values of the property name |
Same with name |
|
n.@ |
Schemas of nodes | string |
|
e ( EDGE ) |
e |
Edges | EDGE |
e.time |
Values of the property time |
Same with time |
|
e.@ |
Schemas of edges | string |
|
p ( PATH ) |
p |
Paths | PATH |
lists ( list ) |
lists |
Lists | list |
lists[2] |
The 3rd elements | Same with the element | |
lists[0:3] |
Lists formed by the 1st to 4th elements | list |
|
lists[:5] |
Lists formed by the 1st to 6th elements | list |
|
lists[2:] |
Lists formed by the 3rd elements to the end | list |
|
points ( point ) |
points |
Points, each with two coordinates | point |
points.x |
Values of the x coordinates | double |
|
points.y |
Values of the y coordinates | double |
More alias referencing formats are supported in the
RETURN
statement.
System Alias
A system alias are designed for special purposes and can be used without declaration. UQL provides three system aliases: prev_n
and prev_e
for inter-step filtering in path templates, and this
to distinguish a property that shares the same name as an alias when referenced in a filter.
prev_n
The alias prev_n
applies exclusively within a node or edge template, referring to the previous node of the current node or edge in a path.
(1) Using prev_n
in the single-node template n()
:
When prev_n
is used in the first n()
of a path template, there is no preceding node for prev_n
to reference. As a result, no results will be returned.
(2) Using prev_n
in the single-edge template e()
:
(3) Using prev_n
in the multi-edge template e()[]
:
(4) Using prev_n
in the multi-edge with intermediates template e().nf()[]
:
Note that all nodes referenced by prev_n
must have the y
property it calls.
This example searches for paths from @actor
nodes to @movie
nodes, where the rating
property of each @movie
node must exceed the score
property of the preceding @actor
node:
n({@actor}).e().n({@movie.rating > prev_n.score}) as p
return p{*}
prev_e
The alias prev_e
applies exclusively within a node or edge template, referring to the previous edge of the current node or edge in a path.
(1) Using prev_e
in the single-node template n()
:
When prev_e
is used in the first n()
of a path template, there is no preceding edge for prev_e
to reference. As a result, no results will be returned.
(2) Using prev_e
in the single-edge template e()
:
When prev_e
is used in the first e()
of a path template, there is no preceding edge for prev_e
to reference. As a result, no results will be returned.
(3) Using prev_e
in the multi-edge template e()[]
:
When prev_e
is used in the first e()[]
of a path template, the first referenced edge does not exist. As a result, no results will be returned.
(4) Using prev_e
in the multi-edge with intermediates template e().nf()[]
:
When prev_e
is used in the edge template of the first e().nf()[]
of a path template, the first referenced edge does not exist. As a result, no results will be returned.
Note that all edges referenced by prev_e
must have the y
property it calls.
This example searches for 4-step outgoing transaction paths between @card
nodes, ensuring ascending transaction times:
n({@card}).re({@transfers}).n({@card})
.re({@transfers.time > prev_e.time})[3]
.n({@card}) as p
return p{*}
this
The alias this
can be used for disambiguation when an alias referenced in a node or edge filter shares the same name as a node or edge property.
In the following example, the alias balance
is declared and balance
is also a node property. Using this.balance
in the filter specifies that the balance
refers to the node property.
... as balance
find().nodes({this.balance > 5000})
...
If {balance > 5000}
is used in this case, it refers to the alias balance
.
Default Alias
The find().nodes()
statement provides a default alias, nodes
, to represent the retrieved nodes. Similarly, the find().edges()
statement provides a default alias, edges
, for the retrieved edges.
You can reference the default alias directly without declaring it.
find().nodes({@account})
return nodes{*}
However, the default alias becomes invalid if a custom alias is declared instead.