Docs Menu
Docs Home
/
MongoDB Atlas
/ / / /

How to Index Boolean Values

On this page

  • Define the Index for the boolean Type
  • Configure boolean Field Properties
  • Try an Example for the boolean Type

You can use the Atlas Search boolean type to index true and false values. You can query fields of type boolean using the equals and in operators. You can also sort by indexed boolean fields.

You can also use the boolean type to index:

  • Fields whose value is an array of booleans. To learn more, see How to Index the Elements of an Array.

  • Boolean fields inside an array of documents indexed as the embeddedDocuments type.

If you enable dynamic mappings, Atlas Search automatically indexes fields of type boolean. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the boolean type.

To define the index for the boolean type, choose your preferred configuration method in the Atlas UI and then select the database and collection.

  1. Click Refine Your Index to configure your index.

  2. In the Field Mappings section, click Add Field Mapping to open the Add Field Mapping window.

  3. Click Customized Configuration.

  4. Select the field to index from the Field Name dropdown.

    Note

    You can't index fields that contain the dollar ($) sign at the start of the field name.

  5. Click the Data Type dropdown and select Boolean. To learn more more about this type, see Field Properties.

  6. Click Add.

The following is the JSON syntax for the boolean type. Replace the default index definition with the following. To learn more about the fields, see Field Properties.

1{
2 "mappings": {
3 "dynamic": false,
4 "fields": {
5 "<field-name>": {
6 "type": "boolean"
7 }
8 }
9 }
10}

The Atlas Search boolean type takes the following parameter:

Option
Type
Necessity
Description
type
string
required
Human-readable label that identifies this field type. Value must be boolean.

The following index definition example uses the sample_guides.planets collection. If you have the sample data already loaded on your cluster, you can use the Visual Editor or JSON Editor in the Atlas UI to configure the index. After you select your preferred configuration method, select the database and collection, and refine your index to add field mappings.

The index definition indexes the hasRings field in the collection as the Atlas Search boolean type to support queries against that field using the Atlas Search equals operator.

  1. In the Add Field Mapping window, select hasRings from the Field Name dropdown.

  2. Click the Data Type dropdown and select Boolean.

  3. Click Add.

Replace the default index definition with the following index definition.

1{
2 "mappings": {
3 "fields": {
4 "hasRings": {
5 "type": "boolean"
6 }
7 }
8 }
9}

Tip

See also: Additional Index Definition Examples

Back

autocomplete