Graph Traversal

Conditional Steps

Filter and conditionally select elements in your graph. These operations allow you to filter and select specific elements from your graph based on conditions.

WHERE

Filter elements based on specific conditions.

::WHERE <condition>

The condition in the WHERE step must evaluate to a boolean value. If the condition is not met, the element will be filtered out.

Example:

QUERY get_adult_users() =>
    // Filter to only users over 18
    adult_users <- N<User>::WHERE(_::{age}::GT(18))
    RETURN adult_users

Comparison Operations

The following operations can be used to compare values.

String, Boolean, and Number Operations

OperationDescriptionExample
::EQ(value)Equals::WHERE(_::{status}::EQ("active"))
::NEQ(value)Not equals::WHERE(_::{age}::NEQ(25))

Number Operations

OperationDescriptionExample
::GT(value)Greater than::WHERE(_::{age}::GT(21))
::LT(value)Less than::WHERE(_::{age}::LT(21))
::GTE(value)Greater than or equal to::WHERE(_::{age}::GTE(21))
::LTE(value)Less than or equal to::WHERE(_::{age}::LTE(21))

EXISTS

Returns true if a traversal has any results. Otherwise, it returns false.

::EXISTS <traversal>

Example:

QUERY get_users_with_followers() =>
    users <- N<User>::WHERE(EXISTS(_::In<Follows>))
    RETURN users

Multiple filter conditions

In the case where you want to filter by multiple conditions, you can use the AND and OR operations to chain the conditions together.

Both AND and OR take a list of expressions that evaluate to a boolean value. AND and OR will return a boolean value themselves.

AND

::WHERE(AND(<condition1>, <condition2>))

OR

::WHERE(OR(<condition1>, <condition2>))

AND Example:

QUERY get_adult_users() =>
    // Filter to only users over 18 AND under 21
    adult_users <- N<User>::WHERE(
        AND(
            _::{age}::GT(18), 
            _::{age}::LT(21)
        )
    )
    RETURN adult_users

OR Example:

QUERY get_other_users() =>
    // Filter to only users under 18 OR over 21
    other_users <- N<User>::WHERE(
        OR(
            _::{age}::LT(18), 
            _::{age}::GT(21)
        )
    )
    RETURN other_users

AND & OR Example:

QUERY get_discounted_users() =>
    // Filter to only users over 18 AND under 21 OR over 65
    discounted_users <- N<User>::WHERE(
        OR(
            AND(
                _::{age}::GT(18), 
                _::{age}::LT(21)
            ),
            _::{age}::GT(65)
        )
    )
    RETURN discounted_users

On this page