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

# Filter Query Results

> Client language: PHP. 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), [Python](https://docs.ravendb.net/6.2/indexes/querying/filtering.python.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.php.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:
>   - [`whereEquals` - Where equals](#whereequals---where-equals)
>   - [`whereGreaterThan` - Numeric property](#wheregreaterthan---numeric-property)
>   - [`whereGreaterThan` - Nested property](#wheregreaterthan---nested-property)
>   - [`whereIn` - Single -vs- Multiple values](#wherein---single--vs--multiple-values)
>   - [`containsAny` - Any value from specified collection](#containsany---any-value-from-specified-collection)
>   - [`containsAll` - All values from specified collection](#containsall---all-values-from-specified-collection)
>   - [`whereStartsWith` - All records with given prefix](#wherestartswith---all-records-with-given-prefix)
>   - [`whereEndsWith` - All records with given suffix](#whereendswith---all-records-with-given-suffix)
>   - [Where - Identifier Property](#where---identifier-property)
>   - [`whereExists` - Where exists or doesn't exist](#whereexists---where-exists-or-doesn)

## `whereEquals` - Where equals

### Query

```php
/** @var array<Employee> $results */
$results = $session
      ->query(Employee::class, Employees_ByFirstAndLastName::class)                // query 'Employees/ByFirstAndLastName' index
      ->whereEquals("FirstName", "Robert")
      ->andAlso()
      ->whereEquals("LastName", "King")    // filtering predicates
      ->toList();                                                      // materialize query by sending it to server for processing
```

### documentQuery

```php
/** @var array<Employee> $results */
$results = $session
  ->advanced()
  ->documentQuery(Employee::class, Employees_ByFirstAndLastName::class)    // query 'Employees/ByFirstAndLastName' index
  ->whereEquals("FirstName", "Robert")                    // filtering predicates
  ->andAlso()                                                  // by default OR is between each condition
  ->whereEquals("LastName", "King")                       // filtering predicates
  ->toList();                                                  // materialize query by sending it to server for processing
```

### Index

```php
class Employees_ByFirstAndLastName extends AbstractIndexCreationTask
{
  public function __construct()
  {
      parent::__construct();

      $this->map = "docs.Employees.Select(employee => new {" .
          "    FirstName = employee.FirstName," .
          "    LastName = employee.LastName" .
          "})";
  }
}
```

### RQL

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

## `whereGreaterThan` - Numeric property

### Query

```php
/** @var array<Product> $results */
$results = $session
  ->query(Product::class, Products_ByUnitsInStock::class)  // query 'Products/ByUnitsInStock' index
  ->whereGreaterThan("UnitsInStock", 50)            // filtering predicates
  ->toList();                                  // materialize query by sending it to server for processing
```

### documentQuery

```php
/** @var array<Product> $results */
$results = $session
  ->advanced()
  ->documentQuery(Product::class, Products_ByUnitsInStock::class)  // query 'Products/ByUnitsInStock' index
  ->whereGreaterThan("UnitsInStock", 50)          // filtering predicates
  ->toList();                                          // materialize query by sending it to server for processing
```

### Index

```php
class Products_ByUnitsInStock extends AbstractIndexCreationTask
{
  public function __construct()
  {
      parent::__construct();

      $this->map = "docs.Products.Select(product => new {" .
          "        UnitsInStock = product.UnitsInStock" .
          "    })";
  }
}
```

### RQL

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

## `whereGreaterThan` - Nested property

### Query

```php
// return all orders that were shipped to 'Albuquerque'
/** @var array<Order> $results */
$results = $session
  ->query(Order::class)
  ->whereEquals("ShipTo_City", "Albuquerque")
  ->toList();
```

### documentQuery

```php
// return all orders that were shipped to 'Albuquerque'
$results = $session
  ->advanced()
  ->documentQuery(Order::class)
  ->whereEquals("ShipTo_City", "Albuquerque")
  ->toList();
```

### RQL

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

### Query

```php
/** @var array<Order> $results */
$results = $session
  ->query(Order::class, Order_ByOrderLinesCount::class)  // query 'Order/ByOrderLinesCount' index
  ->whereGreaterThan("Lines_Count", 50)                               // filtering predicates
  ->toList();                                                                       // materialize query by sending it to server for processing
```

### documentQuery

```php
/** @var array<Order> $results */
$results = $session
  ->advanced()
  ->documentQuery(Order::class, Order_ByOrderLinesCount::class) // query 'Order/ByOrderLinesCount' index
  ->whereGreaterThan("Lines_Count", 50)                                   // filtering predicates
  ->toList();                                                                           // materialize query by sending it to server for processing
```

### Index

```php
class Order_ByOrderLinesCount extends AbstractIndexCreationTask
{
  public function __construct()
  {
      parent::__construct();

      $this->map = "docs.Orders.Select(order => new {" .
          "    Lines_Count = order.Lines.Count" .
          "})";
  }
}
```

### RQL

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

## `whereIn` - Single -vs- Multiple values

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

### Query

```php
/** @var array<Employee> $results */
$results = $session
  ->query(Employee::class, Employees_ByFirstAndLastName::class)    // query 'Employees/ByFirstAndLastName' index
  ->whereIn("FirstName", ["Robert", "Nancy"])    // filtering predicates (remember to add `Raven.Client.Linq` namespace to usings)
  ->toList();                                             // materialize query by sending it to server for processing
```

### documentQuery

```php
/** @var array<Employee> $results */
$results = $session
  ->advanced()
  ->documentQuery(Employee::class, Employees_ByFirstAndLastName::class)    // query 'Employees/ByFirstAndLastName' index
  ->whereIn("FirstName", [ "Robert", "Nancy" ])       // filtering predicates
  ->toList();                                                  // materialize query by sending it to server for processing
```

### Index

```php
class Employees_ByFirstAndLastName extends AbstractIndexCreationTask
{
  public function __construct()
  {
      parent::__construct();

      $this->map = "docs.Employees.Select(employee => new {" .
          "    FirstName = employee.FirstName," .
          "    LastName = employee.LastName" .
          "})";
  }
}
```

### RQL

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

## `containsAny` - Any value from specified collection

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

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

### Query

```php
/** @var array<BlogPost> $results */
$results = $session
  ->query(BlogPost::class, BlogPosts_ByTags::class) // query 'BlogPosts/ByTags' index
  ->containsAny("Tags", [ "Development", "Research" ])                // filtering predicates
  ->toList();                                                                  // materialize query by sending it to server for processing
```

### documentQuery

```php
/** @var array<BlogPost> $results */
$results = $session
  ->advanced()
  ->documentQuery(BlogPost::class, BlogPosts_ByTags::class) // query 'BlogPosts/ByTags' index
  ->containsAny("Tags", [ "Development", "Research" ])                     // filtering predicates
  ->toList();                                                                       // materialize query by sending it to server for processing
```

### Index

```php
class BlogPosts_ByTags extends AbstractIndexCreationTask
{
  public function __construct()
  {
      parent::__construct();

      $this->map = "docs.BlogPosts.Select(post => new {" .
          "    tags = post.tags" .
          "})";
  }
}
```

### RQL

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

## `containsAll` - All values from specified collection

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

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

### Query

```php
/** @var array<BlogPost> $results */
$results = $session
  ->query(BlogPost::class, BlogPosts_ByTags::class) // query 'BlogPosts/ByTags' index
  ->containsAll("Tags", [ "Development", "Research" ])                // filtering predicates
  ->toList();                                                                  // materialize query by sending it to server for processing
```

### documentQuery

```php
/** @var array<BlogPost> $results */
$results = $session
  ->advanced()
  ->documentQuery(BlogPost::class, BlogPosts_ByTags::class)    // query 'BlogPosts/ByTags' index
  ->containsAll("Tags", [ "Development", "Research" ])                        // filtering predicates
  ->toList();                                                                          // materialize query by sending it to server for processing
```

### Index

```php
class BlogPosts_ByTags extends AbstractIndexCreationTask
{
  public function __construct()
  {
      parent::__construct();

      $this->map = "docs.BlogPosts.Select(post => new {" .
          "    tags = post.tags" .
          "})";
  }
}
```

### RQL

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

## `whereStartsWith` - All records with given prefix

### Query

```php
// return all products which name starts with 'ch'
/** @var array<Product> $results */
$results = $session
  ->query(Product::class)
  ->whereStartsWith("Name", "ch")
  ->toList();
```

### documentQuery

```php
// return all products which name starts with 'ch'
/** @var array<Product> $results */
$results = $session
  ->advanced()
  ->documentQuery(Product::class)
  ->whereStartsWith("Name", "ch")
  ->toList();
```

### RQL

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

## `whereEndsWith` - All records with given suffix

### Query

```php
// return all products which name ends with 'ra'
/** @var array<Product> $results */
$results = $session
  ->query(Product::class)
  ->whereEndsWith("Name", "ra")
  ->toList();
```

### documentQuery

```php
// return all products which name ends with 'ra'
/** @var array<Product> $results */
$results = $session
  ->advanced()
  ->documentQuery(Product::class)
  ->whereEndsWith("Name", "ra")
  ->toList();
```

### RQL

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

## Where - Identifier Property

Once a property used in the `whereEquals` 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.php.md).

### Query

```php
/** @var Order $order */
$order = $session
  ->query(Order::class)
  ->whereEquals("Id", "orders/1-A")
  ->firstOrDefault();
```

### documentQuery

```php
/** @var Order $order */
$order = $session
  ->advanced()
  ->documentQuery(Order::class)
  ->whereEquals("Id", "orders/1-A")
  ->firstOrDefault();
```

### RQL

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

### Query

```php
/** @var array<Order> $orders */
$orders = $session
  ->query(Order::class)
  ->whereStartsWith("Id", "orders/1")
  ->toList();
```

### documentQuery

```php
/** @var array<Order> $orders */
$orders = $session
  ->advanced()
  ->documentQuery(Order::class)
  ->whereStartsWith("Id", "orders/1")
  ->toList();
```

### RQL

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

## `whereExists` - 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.php.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.php.md).

## See also

- [Query Overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.php.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.php.md)
- [Paging](https://docs.ravendb.net/6.2/indexes/querying/paging.php.md)
- [Sorting](https://docs.ravendb.net/6.2/indexes/querying/sorting.php.md)
