Filter by Non-Existing Field




Query the collection (dynamic query)

  • You can make a dynamic query on a collection to find which documents are missing the specified field.

  • Use extension methods not & whereExists that are accessible from the query API.

  • 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.

Example

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 
from "orders"
where true and not exists("freight")
// `not` cannot be used immediately after `where`, thus we use `where true`.

Query a static index

  • You can search for documents with missing fields by querying a static index.

  • The index definition must have the following document-fields indexed:

    1. The field that is suspected to be missing in some documents.

    2. A document-field that exists in all documents in the collection,
      (i.e. the id field, or any other field that is common to all).
      Indexing such a field is mandatory so that all documents in the collection will be indexed.

Example

// 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
        }));
    }
}

// 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 
from index "Orders/ByFreight"
where true and not exists("freight")
// `not` cannot come immediately after `where`, thus we use `where true`.

Query by RQL in Studio

  • You can query for documents with missing fields in the Studio's Query view.

  • Use an RQL expression such as:

    from "orders"    
    where exists("company") and not exists("freight")
  • In the where clause,
    first search for a field that exists in every document in the collection,
    and then search for the field that may not exist in some of document.

List Documents Without a Specified Field

Query for documents that are missing the specified field

  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 or press ctrl+enter to run the query.
  5. Index used
    This is 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.
    (Field "Freight" was explicitly removed from these Northwind documents for this example.)