---
title: "Filter by Field Presence"
description: "Find documents that contain a specified field or are missing it, using dynamic queries or static indexes."
url: "https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-by-field-presence"
version: "7.2"
language: "php"
---

# Filter by Field Presence

> Client language: PHP. Also available for [C#](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-by-field-presence.md), [Java](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-by-field-presence.java.md), [Python](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-by-field-presence.python.md), [Node.js](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-by-field-presence.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**
>
> - Documents in the same collection can have different fields.  
>   For example, a field added to newer documents might be missing from older documents.  
>   You can optionally use [schema validation](https://docs.ravendb.net/7.2/documents/schema-validation/overview.md) to require specific fields when documents are written.
>
> - To filter documents by field presence:
>   - To find documents that **contain** a specified field, use `whereExists`.
>   - To find documents that are **missing** a specified field, use `->not()->whereExists`.
>   - Both are available through the [query API](https://docs.ravendb.net/7.2/querying/overview.php.md#query-api).
>
> - An explicit `null` counts as **present** only when it is the value of the field you check.  
>   If a parent on a nested path is missing or `null`, or the field is an empty array, the field counts as **missing**.  
>   Learn more in [How field presence is determined](#how-field-presence-is-determined).
>
> - You can check field presence when querying a **static index** or when making a **dynamic query**.  
>   When querying a static index, the check applies to the fields produced by the index's map function.
>
> - In this article:
>   - [Find documents that CONTAIN a field](#find-documents-that-contain-a-field)
>     - [Dynamic query](#dynamic-query-existing-field)
>     - [Static index query](#static-index-query-existing-field)
>   - [Find documents that are MISSING a field](#find-documents-that-are-missing-a-field)
>     - [Dynamic query](#dynamic-query-missing-field)
>     - [Static index query](#static-index-query-missing-field)
>   - [How field presence is determined](#how-field-presence-is-determined)
>   - [Syntax](#syntax)

## Find documents that CONTAIN a field {#find-documents-that-contain-a-field}

### Dynamic query /\* #dynamic-query-existing-field \*/

- Use `whereExists` to return documents that contain the specified field.  
  Documents that are missing the field are excluded from the results.

- RavenDB uses an auto-index to process the filter, creating one if needed.  
  Learn more about this query type in [Query a collection - using an auto-index](https://docs.ravendb.net/7.2/querying/overview.php.md#2-query-a-collection---using-an-auto-index).

- You can check field presence using either a **field name** or a nested **field path**, as shown below.

---

#### Filter documents by field name

The following query returns employees whose documents contain the `FirstName` field:

##### Query

```php
$employees = $session
    ->query(Employee::class)
    ->whereExists("FirstName")
    ->toList();
```

##### RQL

```sql
from Employees
where exists("FirstName")
```

#### Filter documents by nested field path

You can also check for a nested field by specifying its full path.  
The following query returns employees whose documents contain `Latitude` at `Address.Location.Latitude`:

##### Query

```php
$employees = $session
    ->query(Employee::class)
    ->whereExists("Address.Location.Latitude")
    ->toList();
```

##### RQL

```sql
from Employees
where exists(Address.Location.Latitude)
```

---

### Static index query /\* #static-index-query-existing-field \*/

- When querying a static index, use the **index-field name** in `whereExists`.  
  The index must include the field whose presence you want to check.

- The following index maps:
  - `FirstName` to an index-field with the same name.
  - `Address.Location.Latitude` to an index-field named `Latitude`.
  - The document ID to an index-field named `Id`, to retain entries for documents with missing fields.  
    See [Static index query for missing fields](#static-index-query-missing-field) for details.

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

            // The index-fields: Id, FirstName, Latitude
            // Include Id so documents with missing fields still have an index entry.
            $this->map = "from employee in docs.Employees " .
                         "select new { " .
                         "    Id = employee.Id, " .
                         "    FirstName = employee.FirstName, " .
                         "    Latitude = employee.Address.Location.Latitude " .
                         "}";
        }
    }
    ```

---

#### Filter documents by field name

The following example queries the `Employees_ByFieldPresence` index and returns employee documents that contain the `FirstName` field:

##### Query

```php
$employees = $session
    ->query(Employee::class, Employees_ByFieldPresence::class)
    ->whereExists("FirstName")
    ->toList();
```

##### RQL

```sql
from index "Employees/ByFieldPresence"
where exists("FirstName")
```

#### Filter documents by a mapped nested field

The index maps the nested document field `Address.Location.Latitude` to an index-field named `Latitude`.  
Use `Latitude` in the query:

##### Query

```php
$employees = $session
    ->query(Employee::class, Employees_ByFieldPresence::class)
    ->whereExists("Latitude")
    ->toList();
```

##### RQL

```sql
from index "Employees/ByFieldPresence"
where exists("Latitude")
```

## Find documents that are MISSING a field {#find-documents-that-are-missing-a-field}

### Dynamic query /\* #dynamic-query-missing-field \*/

- Use `->not()->whereExists` to return documents that are missing the specified field.
- You can check for a missing field using either a **field name** or a nested **field path**, as shown below.

---

#### Filter documents by field name

The following query returns employees whose documents do **not** contain the field `FirstName`:

##### Query

```php
$employeesWithoutFirstName = $session
    ->query(Employee::class)
    ->not()
    ->whereExists("FirstName")
    ->toList();
```

##### RQL

```sql
from Employees
where true and not exists("FirstName")
```

In RQL, `not` cannot appear immediately after `where`.  
Use `where true and not exists(...)` when the missing-field condition is the first condition.  
`$session->query` adds `true and` automatically in this case.

#### Filter documents by nested field path

Use the full path to check for a missing nested field.  
The following query returns employees whose documents do **not** contain `Latitude` at `Address.Location.Latitude`:

##### Query

```php
$employeesWithoutLatitude = $session
    ->query(Employee::class)
    ->not()
    ->whereExists("Address.Location.Latitude")
    ->toList();
```

##### RQL

```sql
from Employees
where true and not exists(Address.Location.Latitude)
```

---

### Static index query /\* #static-index-query-missing-field \*/

- Use `->not()->whereExists` with the **index-field name**.  
  The field you want to check must be included in the index's map function.

- The index must also retain an entry for documents that are missing that field.
  The index used in this article maps the document ID to `Id`, ensuring that an entry is retained even when both `FirstName` and `Latitude` are missing.

- **Alternatively**, set [Indexing.IndexEmptyEntries](https://docs.ravendb.net/7.2/server/configuration/indexing-configuration.md#indexingindexemptyentries) to `true` to retain entries for documents that are missing all mapped fields,
  without needing to map `Id` for this purpose.

> **Note**
>
> If [Indexing.IndexMissingFieldsAsNull](https://docs.ravendb.net/7.2/server/configuration/indexing-configuration.md#indexingindexmissingfieldsasnull) is `true`,
> missing mapped fields are indexed as `null` and count as **present**.  
> Consequently, `->not()->whereExists` does not return documents whose missing fields were indexed this way.  
> This setting does not affect auto-indexes.

---

#### Filter documents by field name

Query the `Employees_ByFieldPresence` index defined [above](#static-index-query-existing-field):

##### Query

```php
$employeesWithoutFirstName = $session
    ->query(Employee::class, Employees_ByFieldPresence::class)
    ->not()
    ->whereExists("FirstName")
    ->toList();
```

##### RQL

```sql
from index "Employees/ByFieldPresence"
where true and not exists("FirstName")
```

#### Filter documents by a mapped nested field

The index maps the document field `Address.Location.Latitude` to an index-field named `Latitude`.  
The following query checks the `Latitude` index-field and returns employee documents that do **not** contain `Latitude` at `Address.Location.Latitude`:

##### Query

```php
$employeesWithoutLatitude = $session
    ->query(Employee::class, Employees_ByFieldPresence::class)
    ->not()
    ->whereExists("Latitude")
    ->toList();
```

##### RQL

```sql
from index "Employees/ByFieldPresence"
where true and not exists("Latitude")
```

## How field presence is determined {#how-field-presence-is-determined}

- `whereExists` checks field presence in the index used by the query:
  - An explicit `null` counts as **present** only when it is the value of the field you check.
  - If a parent on a nested path is missing or `null`, the field counts as **missing**.
  - An empty array counts as **missing**.

- These rules apply to both query types, with a configuration exception for static indexes:

  - **For dynamic queries**, they apply to the document field name or nested path specified in the query.

  - **For static index queries**, presence is checked on the fields produced by the index's map function.  
    For example, with the direct index-field mapping `Latitude = employee.Address.Location.Latitude`,  
    the query checks `Latitude`, not `Address.Location.Latitude`.

    If `Address` is missing or `null`, this mapping produces an implicit `null` for `Latitude`,  
    which is not indexed by default. The `Latitude` index-field therefore counts as **missing**.

    The [Indexing.IndexMissingFieldsAsNull](https://docs.ravendb.net/7.2/server/configuration/indexing-configuration.md#indexingindexmissingfieldsasnull) configuration key is `false` by default.  
    If set to `true`, missing mapped fields are indexed as `null` and count as **present**.  
    Empty arrays still count as **missing**, regardless of this setting.

    For `->not()->whereExists` to return documents with missing fields, the index must retain entries for them.  
    See [Static index query for missing fields](#static-index-query-missing-field) for details.

---

The following examples use document field names and paths and assume the default indexing settings.  
For static indexes, they assume direct mappings and retained entries for documents with missing fields,  
as explained above.

| Document content | Checked path | `whereExists` result | `->not()->whereExists` result |
| - | - | - | - |
| `"FirstName": null` | `FirstName` | Document returned ✅ | Document excluded ❌ |
| No `FirstName` field | `FirstName` | Document excluded ❌ | Document returned ✅ |
| `"Territories": []` | `Territories` | Document excluded ❌ | Document returned ✅ |
| `"Address": { "Location": { "Latitude": null } }` | `Address.Location.Latitude` | Document returned ✅ | Document excluded ❌ |
| `"Address": null` | `Address.Location.Latitude` | Document excluded ❌ | Document returned ✅ |
| `"Address": { "City": "Seattle" }` | `Address.Location.Latitude` | Document excluded ❌ | Document returned ✅ |

## Syntax {#syntax}

### whereExists

```php
function whereExists(string $fieldName): DocumentQueryInterface;
```

| Parameter | Type | Description |
| - | - | - |
| **fieldName** | `string` | For a dynamic query, the document field name or nested path.<br />For a static index query, the index field name. |

---

### not

The `not()` method negates the next filtering condition.  
Place it immediately before `whereExists` to search for missing fields:

```php
->not()->whereExists("FirstName")
```

---

### RQL

```sql
// Find documents with the field.
where exists(FieldName)

// Find documents without the field.
where true and not exists(FieldName)
```

## See also

- [Filter Query Results](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-query-results.php.md)
- [What is a Document Query](https://docs.ravendb.net/7.2/querying/document-query/what-is-document-query.php.md)
- [Query an Index](https://docs.ravendb.net/7.2/indexes/querying/query-index.php.md)
