Docs Menu
Docs Home
MongoDB Atlas
/ / / /

How to Index the Elements of an Array

On this page

  • How Does Atlas Search Index Array Elements?
  • How Can I Index Objects in Arrays?
  • Review the Limitations
  • Define the Index for the Array Elements
  • Try an Example for Indexing Array Elements

For indexing arrays, Atlas 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, Atlas Search automatically indexes elements of dynamically indexable data types inside the array. To learn more about the data types that Atlas Search dynamically indexes, see Data Types.

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

Atlas Search indexes the supported data types inside an array by flattening the fields during indexing.


Consider the following documents:

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

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

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

To query fields inside an array of documents or objects, you must use the embeddedDocuments type to index the field that contains the array of objects.

Atlas Search doesn't index the following Atlas Search field types if the field type is contained in an array or is in a document that is contained in an array:

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

The following index definition example uses the sample_mflix.movies 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 following index definition indexes the genres field, which contains an array of string values.

← 2: Define Field Mappings