Using OPTIONAL
GQL provides the OPTIONAL
keyword to ensure the statements execute continuously even when no data is available to drive subsequent statements.
In general, OPTIONAL
assigns a NULL
value when the statement cannot produce any result.
OPTIONAL MATCH
The MATCH
statement in GQL supports the OPTIONAL
keyword.
For example, to retrieve nodes with some IDs from the graph shown in Figure 7.5, use the following:
GQL:
FOR id in ["a", "b", "z"]
OPTIONAL MATCH (n {_id: id})
RETURN n
The query returns the following:
a
b
NULL
Since the node z
does not exist, the result corresponding to the ID z
is NULL
. If the query is executed without the OPTIONAL
keyword, it will return only the matched results:
GQL:
FOR id in ["a", "b", "z"]
MATCH (n {_id: id})
RETURN n
This query returns the following:
a
b
Another example is to retrieve optional...