---
title: "Filter Query Results"
url: "https://docs.ravendb.net/6.2/indexes/querying/filtering"
version: "6.2"
language: "python"
---

# Filter Query Results

> Client language: Python. Also available for [C#](https://docs.ravendb.net/6.2/indexes/querying/filtering.md), [Java](https://docs.ravendb.net/6.2/indexes/querying/filtering.java.md), [PHP](https://docs.ravendb.net/6.2/indexes/querying/filtering.php.md), [Node.js](https://docs.ravendb.net/6.2/indexes/querying/filtering.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - One of the most basic functionalities of querying is the ability to filter out data and return records that match a given condition.
>
> - Index queries can be executed using -
>   - [query\_index\_type](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.python.md) from the basic `session` API
>   - [RQL](https://docs.ravendb.net/6.2/client-api/session/querying/what-is-rql.md) - Raven Query Language
>
> - The examples in this page demonstrate how filtering is applied by each of these methods.
>
> - In this page:
>   - [`where_equals` - Where equals](#where_equals---where-equals)
>   - [`where_greater_than` - Numeric property](#where_greater_than---numeric-property)
>   - [`where_greater_than` - Nested property](#where_greater_than---nested-property)
>   - [`where_in` - Single -vs- Multiple values](#where_in---single--vs--multiple-values)
>   - [`contains_any` - Any value from specified collection](#contains_any---any-value-from-specified-collection)
>   - [`contains_all` - All values from specified collection](#contains_all---all-values-from-specified-collection)
>   - [`where_starts_with` - All records with given prefix](#where_starts_with---all-records-with-given-prefix)
>   - [`where_ends_with` - All records with given suffix](#where_ends_with---all-records-with-given-suffix)
>   - [Where - Identifier Property](#where---identifier-property)
>   - [`where_exists` - Where exists or doesn't exist](#where_exists---where-exists-or-doesn)

## `where_equals` - Where equals

### Query

```python
results = list(  # Materialize query by sending it to server for processing
  session.query_index_type(
      Employees_ByFirstAndLastName, Employee
  )  # query 'Employees/ByFirstAndLastName' index
  .where_equals("FirstName", "Robert")  # filtering predicates
  .and_also()
  .where_equals("LastName", "King")
)
```

### Index

```python
class Employees_ByFirstAndLastName(AbstractIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.map = "from e in docs.Employees select new {FirstName = e.FirstName, LastName = e.LastName}"
```

### RQL

```sql
from index 'Employees/ByFirstAndLastName'
where FirstName = 'Robert' and LastName = 'King'
```

## `where_greater_than` - Numeric property

### Query

```python
results = list(  # Materialize query by sending it to server for processing
  session.query_index_type(
      Products_ByUnitsInStock, Product  # query 'Products/ByUnitsInStock' index
  ).where_greater_than(
      "UnitsInStock", 50
  )  # filtering predicates
)
```

### Index

```python
class Products_ByUnitsInStock(AbstractIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.map = "from p in docs.Products select new {p.UnitsInStock}"
```

### RQL

```sql
from index 'Products/ByUnitsInStock'
where UnitsInStock > 50
```

## `where_greater_than` - Nested property

### Query

```python
# return all orders that were shipped to 'Albuquerque'
results = list(session.query(object_type=Order).where_equals("ShipTo.City", "Albuquerque"))
```

### RQL

```sql
from Orders
where ShipTo.City = 'Albuquerque'
```

### Query

```python
results = list(  # Materialize query by sending it to server for processing
  session.query_index_type(
      Order_ByOrderLinesCount, Order  # query 'Orders/ByOrderLinesCount' index
  ).where_greater_than(
      "Lines.Count", 50
  )  # filtering predicates
)
```

### Index

```python
class Order_ByOrderLinesCount(AbstractIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.map = "from o in docs.Orders select new {Lines_Count = order.Lines.Count}"
```

### RQL

```sql
from index 'Order/ByOrderLinesCount'
where Lines.Count > 50
```

## `where_in` - Single -vs- Multiple values

When you want to check a single value against multiple values, `where_in` can become handy.  
To retrieve all employees where `FirstName` is either `Robert` or `Nancy`, we can issue the following query:

### Query

```python
results = list(  # Materialize query by sending it to server for processing
  session.query_index_type(
      Employees_ByFirstAndLastName, Employee  # query 'Employees/ByFirstAndLastName' index
  ).where_in(
      "FirstName", ["Robert", "Nancy"]
  )  # filtering predicates
)
```

### Index

```python
class Employees_ByFirstAndLastName(AbstractIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.map = "from e in docs.Employees select new {FirstName = e.FirstName, LastName = e.LastName}"
```

### RQL

```sql
from index 'Employees/ByFirstAndLastName'
where FirstName IN ('Robert', 'Nancy')
```

## `contains_any` - Any value from specified collection

To check if enumeration contains **any** of the values from a specified collection,
use the `contains_any` method.

For example, if you want to return all `BlogPosts` that contain any of the specified `Tags`:

### Query

```python
results = list(  # Materialize query by sending it to server for processing
  session.query_index_type(BlogPosts_ByTags, BlogPost).contains_any(  # query 'BlogPosts/ByTags' index
      "Tags", ["Development", "Research"]
  )  # filtering predicates
)
```

### Index

```python
class BlogPosts_ByTags(AbstractIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.map = "from post in posts select new {post.Tags}"
```

### RQL

```sql
from index 'BlogPosts/ByTags'
where Tags IN ('Development', 'Research')
```

## `contains_all` - All values from specified collection

To check if an enumeration contains **all** of the values from a specified collection,
use the `contains_all` method.

For example, if you want to return all the `BlogPosts` that contain all of the specified `Tags`:

### Query

```python
results = list(  # Materialize query by sending it to server for processing
  session.query_index_type(BlogPosts_ByTags, BlogPost).contains_all(  # query 'BlogPosts/ByTags' index
      "Tags", ["Development", "Research"]
  )  # filtering predicates
)
```

### Index

```python
class BlogPosts_ByTags(AbstractIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.map = "from post in posts select new {post.Tags}"
```

### RQL

```sql
from index 'BlogPosts/ByTags'
where Tags ALL IN ('Development', 'Research')
```

## `where_starts_with` - All records with given prefix

### Query

```python
# return all products which name starts with 'ch'
results = list(session.query(object_type=Product).where_starts_with("Name", "ch"))
```

### RQL

```sql
from Products 
where startsWith(Name, 'ch')
```

## `where_ends_with` - All records with given suffix

### Query

```python
results = list(session.query(object_type=Product).where_ends_with("Name", "ra"))
```

### RQL

```sql
from Products 
where endsWith(Name, 'ra')
```

## Where - Identifier Property

Once a property used in the `where_equals` clause is recognized as an identity property of a given entity type
(according to [`FindIdentityProperty` convention](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/global.md#findidentityproperty))
and there aren't any other fields involved in the query, then it is called a "collection query".
Simple collection queries that ask about documents with given IDs or where identifiers start with a given prefix
and don't require any additional handling like ordering, full-text searching, etc, are handled directly by the storage engine.
It means that querying by ID doesn't create an auto-index and has no extra cost. In terms of efficiency, it is the same as
loading documents using [`session.load`](https://docs.ravendb.net/6.2/client-api/session/loading-entities.python.md).

### Query

```python
order = session.query(object_type=Order).where_equals("Id", "orders/1-A").first()
```

### RQL

```sql
from Orders
where id() = 'orders/1-A'
```

### Query

```python
orders = list(session.query(object_type=Order).where_starts_with("Id", "orders/1"))
```

### RQL

```sql
from Orders
where startsWith(id(), 'orders/1')
```

## `where_exists` - Where exists or doesn't exist

To find all documents in a collection that have a specified field, see [How to Filter by Field Presence](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-field.python.md).

To find all documents in a collection that don't have a specified field, see [How to Filter by Non-Existing Field](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-non-existing-field.python.md).

## See also

- [Query Overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.python.md)
- [Indexing Basics](https://docs.ravendb.net/6.2/indexes/indexing-basics.md)
- [Query an Index](https://docs.ravendb.net/6.2/indexes/querying/query-index.python.md)
- [Paging](https://docs.ravendb.net/6.2/indexes/querying/paging.python.md)
- [Sorting](https://docs.ravendb.net/6.2/indexes/querying/sorting.python.md)
