Count Query Results



count

Count query results using the count method.

number_of_orders = (
    session.advanced.document_query(object_type=Order).where_equals("ship_to.country", "UK").count()
)
# The query returns the NUMBER of orders shipped to UK (int)
from "Orders"
where ship_to.country == "UK" limit 0, 0

// The RQL generated will trigger query execution
// however, no documents are returned (limit is set 0)

Get count from query stats

When executing a query, you can retrieve query statistics that include the total number of results.
To do this, define a callback function that takes QueryStatistics as an argument and applies whatever logic you want to apply.

def **statistics_callback(statistics: QueryStatistics) -> None:
   # Read and interact with QueryStatistics here
   total_results = statistics.total_results
   duration_milliseconds = statistics.duration_in_ms
   ...

Then pass your function as an argument to the query.statistics method and use the retrieved QueryStatistics object.

employees = list(
   session.query(object_type=Employee)
   .where_equals("first_name", "Robert")
   .statistics(**statistics_callback)
)

Learn more in Get Query Statistics.