Docs Menu
Docs Home
/
MongoDB Atlas
/ /

How to Check for Null and Non-Null Values with Atlas Search

On this page

  • Insert Sample Data
  • Create an Atlas Search Index with Dynamic Mapping
  • Check for null Values
  • Find All Non-null Values

This tutorial describes how to use Atlas Search queries to check your data for null or non-null values. It takes you through the following steps:

  • Load sample documents with null values and missing fields into the sample_mflix.users collection.

  • Set up an Atlas Search index with dynamic mapping for the sample_mflix.users collection.

  • Run an Atlas Search query to find all documents with null values for the password field in the sample_mflix.users collection.

    Note

    To successfully check for null values, the field that you are querying can't have polymorphic data. In this example, all documents in the sample_mflix.users collection have the string data type for the password field.

  • Run an Atlas Search query to find all documents that don't contain null values for the password field in the sample_mflix.users collection.

Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.

To create an Atlas Search index, you must have Project Data Access Admin or higher access to the project.

In this section, you add sample documents that contain null values and missing fields to the sample_mflix.users collection. These additional documents allow you to successfully run the sample queries for null and non-null values in this tutorial.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. If the Clusters page is not already displayed, click Database in the sidebar.

2
3
  1. In the sample_mflix database, select the users collection.

  2. Click Insert Document.

  3. Click the JSON view ({}) to replace the default document.

  4. One at a time, copy and paste the following sample documents and click Insert to add the documents to the collection.

    { "name": "Andre Robinson", "email": "andre.robinson@example.com", "password": null }
    { "name": "Laura Garcia", "email": "lgarcia@example.net" }

In this section, you create an Atlas Search index that uses dynamic mapping to automatically index all the dynamically indexable fields in the sample_mflix.users collection.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. Click your cluster's name.

  4. Click the Atlas Search tab.

2
3
  • For a guided experience, select the Atlas Search Visual Editor.

  • To edit the raw index definition, select the Atlas Search JSON Editor.

4
  1. In the Index Name field, enter null-check-tutorial.

    Note

    If you name your index default, you don't need to specify an index parameter when using the $search pipeline stage. Otherwise, you must specify the index name using the index parameter.

  2. In the Database and Collection section, find the sample_mflix database, and select the users collection.

5

You can create an Atlas Search index that uses dynamic mappings through the Atlas Search Visual Editor or Atlas Search JSON Editor in the Atlas User Interface. The index definition that you create dynamically indexes the fields of supported types in each document in the users collection.

6
7

A modal window appears to let you know your index is building. Click the Close button.

8

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.


Use the Select your language drop-down menu to set the language of the example on this page.


After setting up the Atlas Search index, you can connect to your Atlas cluster and run queries against fields in the sample_mflix.users collection. The following query uses the compound and wildcard operators to find all documents that have a password field with a null value.

Note

We don't recommend using the wildcard operator to query on collections with large index sizes. To learn more and vote for Atlas Search enhancements to support indexing and querying for null datatype, use the MongoDB Feedback Engine.

Atlas Search returns one document with a password value that is null. Atlas Search doesn't include documents that are missing the password field because the query specifies that the password field must exist.


Use the Select your language drop-down menu to set the language of the example on this page.


After setting up the Atlas Search index, you can connect to your Atlas cluster and run queries against fields in the sample_mflix.users collection. The following query uses the compound and wildcard operators to find documents that don't have a null value in the password field or don't have the password field.

Atlas Search returns documents with password values that are not null. The first document listed in the results has a higher score because the constant option alters the score for documents with a missing password field.

← Search UI