array-type

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset

Available tabs: other tabs: vib, jsonib -->

How to Index the Elements of an Array

For indexing arrays, MongoDB Search requires only the data type of the array elements. You don't have to specify that the data is contained in an array ([]) in the index definition.

If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes elements of dynamically indexable data types inside the array. To learn more about the data types that MongoDB Search dynamically indexes, see [MongoDB Search Field Types.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-bson-data-types

You can use the Visual Editor or the JSON Editor in the Atlas UI to define the index for elements in arrays.

How Does MongoDB Search Index Array Elements?

MongoDB Search indexes the [supported data types](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-bson-data-chart inside an array by flattening the fields during indexing.

Example:

Consider the following documents:

doc1 = { a: {b: [[<value1>, <value2>], <value3>] }}
doc2 = { a: {b: [<value1>, <value2>, <value3>] } }
doc3 = { a: [{ b: <value1>}, {b: <value2>}, {b: <value3>}] }

MongoDB Search flattens the preceding arrays similar to the following during indexing:

LuceneDoc<n> = {"a.b":[<value1>,<value2>,<value3>]}

How Can I Index Objects in Arrays?

To query fields inside an array of documents or objects, you must use the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type to index the field that contains the array of objects.

Define the Index for the Array Elements

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

Visual Editor

  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 of type array to index from the Field Name dropdown.

  5. Click the Data Type dropdown and select the data type of the array element that you want to index. To learn more about the configuring the properties for the selected type, refer to the documentation for the selected type.

  6. Click Add.

Try an Example for Indexing Array Elements

The following index definition example uses the [sample_mflix.movies](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix collection. If you have the [sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-sample-data already loaded on your cluster, you can use the Visual Editor or JSON (Javascript Object Notation) 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 following index definition indexes the genres field, which contains an array of string values.

Visual Editor

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

  2. Click the Data Type dropdown and select String.

  3. Keep the default settings for the String Properties.

  4. Click Add.