Skip to main content

How to filter data

You can use the methods where(), where_not(), or_where(), or_where_not(), and_where(), and_where_not(), xor_where() and xor_where_not() to construct queries that will filter data.

  • where(self, item: str, operator: Operator, **kwargs) - Filter data so that operator evaluates the comparison of item and literal or expression to true.
  • where_not(self, item: str, operator: Operator, **kwargs) - Filter data so that operator evaluates the comparison of item and literal or expression to false.
  • or_where(self, item: str, operator: Operator, **kwargs) - Append an additional filter with OR.
  • or_not_where(self, item: str, operator: Operator, **kwargs) - Append an additional filter with OR NOT.
  • and_where(self, item: str, operator: Operator, **kwargs) - Append an additional filter with AND.
  • and_not_where(self, item: str, operator: Operator, **kwargs) - Append an additional filter with AND NOT.
  • xor_where(self, item: str, operator: Operator, **kwargs) - Append an additional filter with XOR.
  • xor_not_where(self, item: str, operator: Operator, **kwargs) - Append an additional filter with XOR NOT.

In this guide, you'll learn how to:

Filter data by property comparison

You can filter data by comparing properties of graph objects. Below you can see how to compare name properties of two nodes.

from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator

query = Match()
.node(labels="Person", variable="p1")
.to(relationship_type="FRIENDS_WITH")
.node(labels="Person", variable="p2")
.where(item="p1.name", operator=Operator.EQUAL, expression="p2.name")
.return_()
.execute()

Here the expression keyword argument is used because the property shouldn't be quoted in the Cypher query.

Standard boolean operators like NOT, AND, OR and XOR are used in the Cypher query language. To have NOT within WHERE clause, you need to use where_not() method.

from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator

query = Match()
.node(labels="Person", variable="p1")
.to(relationship_type="FRIENDS_WITH")
.node(labels="Person", variable="p2")
.where_not(item="p1.name", operator=Operator.EQUAL, expression="p2.name")
.return_()
.execute()

In a similar way, you can use AND and AND NOT clauses, which correspond to the methods and_where() and and_not_where(). Using the query below you can find all persons with the same first_name and last_name, but different address.

from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator

query = Match()
.node(labels="Person", variable="p1")
.to(relationship_type="FRIENDS_WITH")
.node(labels="Person", variable="p2")
.where(item="p1.first_name", operator=Operator.EQUAL, expression="p2.first_name")
.and_where(item="p1.last_name", operator=Operator.EQUAL, expression="p2.last_name")
.and_not_where(item="p1.address", operator=Operator.EQUAL, expression="p2.address")
.return_()
.execute()

The same goes for the OR, OR NOT, XOR and XOR NOT clauses, which correspond to the methods or_where(), or_not_where(), xor_where() and xor_not_where().

Filter data by property value

You can filter data by comparing the property of a graph object to some value (a literal). Below you can see how to compare age property of a node to the integer.

from gqlalchemy import Match

query = Match()
.node(labels="Person", variable="p")
.where(item="p.age", operator=">", literal=18)
.return_()
.execute()

It's important that the third keyword argument is literal, since then, it will be of the correct type.

Just like in property comparison, you can use different boolean operators to further filter the data.

from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator

query = Match()
.node(labels="Person", variable="p")
.where(item="p.age", operator=Operator.GREATER_THAN, literal=18)
.or_where(item="p.name", operator=Operator.EQUAL, literal="John")
.return_()
.execute()

The literal keyword is used again since you want John to be quoted in the Cypher query.

Filter data by label

Nodes can be filtered by their label using the WHERE clause instead of specifying it directly in the MATCH clause. You have to use expression as the third keyword argument again since you don't want the quotes surrounding the label in the Cypher clause.

Below you can see an example of how to filter data by label:

from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator

query = Match()
.node(variable="p")
.where(item="p", operator=Operator.LABEL_FILTER, expression="Person")
.return_()
.execute()

Just like in property comparison, you can use different boolean operators to further filter the data.