---
title: "Filter by Non-Existing Field"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-non-existing-field"
version: "6.2"
language: "nodejs"
---

# Filter by Non-Existing Field

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-non-existing-field.md), [Python](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-non-existing-field.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-non-existing-field.php.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**
>
> - There are situations where new fields are added to some documents in a collection over time.
>
> - To find the documents that are missing the newly added fields you can either:
>   - [Query the collection (dynamic query)](#query-the-collection-\(dynamic-query\))
>   - [Query a static index](#query-a-static-index)
>   - [Use Studio to Run an RQL Query](#use-studio-to-run-an-rql-query)
>
> ---

## Query the collection (dynamic query)

To run a dynamic query over a collection and find which documents are missing a specified field,  
use the `not` and `whereExists` extension methods, accessible from the [query](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.nodejs.md) API,
as shown below.

This will either create a new auto-index or add the queried field to an existing auto-index.  
Learn more about the dynamic query flow [here](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.nodejs.md#dynamicquery).

**Example**

### DocumentQuery

```javascript
const ordersWithoutFreightField = await session
   // Define a query on 'orders' collection
  .query({ collection: "orders" })
   // Search for documents that do Not contain field 'freight'
  .not()
  .whereExists("freight")
   // Execute the query
  .all();

// Results will be only the documents that do Not contain the 'freight' field in 'orders' collection
```

### RQL

```sql
from "orders"
where true and not exists("freight")
// `not` cannot be used immediately after `where`, thus we use `where true`.
```

## Query a static index

Documents with missing fields can be searched by querying a static index.

The index definition must contain the following document fields indexed:

- A document field that **exists** in **all** documents of the queried collection, e.g. the *Id* field.  
  Indexing this field will ensure that all the documents of this collection are indexed.
- A document field that is suspected to be **missing** from some documents of the queried collection.

**Example**

```javascript
// Define a static index on the 'orders' collection
// ================================================

class Orders_ByFreight extends AbstractJavaScriptIndexCreationTask<Order> {

  constructor() {
      super();

      // Define the index-fields 
      this.map("orders", o => ({
          // Index a field that might be missing in SOME documents
          freight: o.firstName,
          // Index a field that exists in ALL documents in the collection
          id: o.lastName
      }));
  }
}
```

### DocumentQuery

```javascript
// Query the index
// ===============

const employees = await session
   // Define a query on the index
  .query({ indexName: "Orders/ByFreight" })
   // Search for documents that do Not contain field 'freight'
  .not()
  .whereExists("freight")
   // Execute the query
  .all();

// Results will be only the documents that do Not contain the 'freight' field in 'orders' collection
```

### RQL

```sql
from index "Orders/ByFreight"
where true and not exists("freight")
// `not` cannot come immediately after `where`, thus we use `where true`.
```

## Use Studio to Run an RQL Query

- Documents can be searched by missing fields using Studio's [Query view](https://docs.ravendb.net/6.2/studio/database/queries/query-view.md).

- Use an [RQL](https://docs.ravendb.net/6.2/client-api/session/querying/what-is-rql.md) expression such as:

```sql
from "Orders"    
where exists("Company") and not exists("Freight")
```

- In the `where` clause:  
  First search for a field that **exists** in **all** documents of the queried collection, e.g. the *Id* field.  
  Then search for a field that **may be missing** from some documents of the queried collection.

  ![List Documents Without a Specified Field](https://docs.ravendb.net/assets/images/non-existing-field-studio-rql-cd11cbe343278adc501d34bb4b834256.png)

  1. **Indexes**  
     Click to see the Indexes menu.
  2. **Query**  
     Select to open the Query view.
  3. **Query editor**  
     Write the RQL query.
  4. **Run Query**  
     Click to run the query.
  5. **Index used**  
     The name of the auto-index created to serve this query.  
     You can click it to see the available Studio options for this index.
  6. **Results**  
     This is the list of documents that do not contain the specified 'Freight' field.  
     (the "Freight" Field was removed from these Northwind documents for this example.)

## See also

- [What is a Document Query](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/what-is-document-query.nodejs.md)
- [How to Filter by Field Presence](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-field.nodejs.md)
- [Querying: Filtering](https://docs.ravendb.net/6.2/indexes/querying/filtering.nodejs.md)
- [Query vs DocumentQuery](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/query-vs-document-query.nodejs.md)
- [RQL - Raven Query Language](https://docs.ravendb.net/6.2/client-api/session/querying/what-is-rql.md)
- [Queries - Filtering Results - Basics](https://demo.ravendb.net/demos/csharp/queries/filtering-results-basics)
- [Queries - Filtering with Multiple Conditions](https://demo.ravendb.net/demos/csharp/queries/filtering-results-multiple-conditions)
- [Queries - Filtering Results - Basics](https://demo.ravendb.net/demos/python/queries/filtering-results-basics)
- [Queries - Filtering with Multiple Conditions](https://demo.ravendb.net/demos/python/queries/filtering-results-multiple-conditions)
