How to Index Fields in Objects and Documents
On this page
You can use the Atlas Search document
type to index fields in objects or
documents.
If you enable dynamic mappings, Atlas Search
automatically indexes fields of type document
. You can
use the Visual Editor or the JSON Editor in the Atlas UI to
index fields as the document
type.
Review document
Type Limitations
You can't use the Atlas Search document
type to index fields in objects or
documents that are inside an array. Instead, use the Atlas Search
embeddedDocuments type to
index fields in objects or documents that are elements of an array.
Define the Index for the document
Type
To define the index for the document
type, choose your preferred
configuration method in the Atlas UI and then select the
database and collection.
Click Refine Your Index to configure your index.
In the Field Mappings section, click Add Field to open the Add Field Mapping window.
Click Customized Configuration.
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.Click the Data Type dropdown and select Document.
Toggle the Enable Dynamic Mapping setting to enable or disable dynamic indexing of all dynamically indexable fields in the document. To learn more, see Configure
document
Field Properties.Click Add.
If you disabled dynamic mapping, click Add Child Field for the Document type field to define field mappings for the fields in the document.
The following is the JSON syntax for the document
type.
Replace the default index definition with the following. To learn more
about the fields, see Field Properties.
1 { 2 "mappings": { 3 "dynamic": true|false, 4 "fields": { 5 "<field-name>": { 6 "type": "document", 7 "dynamic": true|false, 8 "fields": { 9 "<field-name>": { 10 <field-mapping-definition> 11 } 12 } 13 } 14 } 15 } 16 }
Configure document
Field Properties
The Atlas Search document
type takes the following parameters:
Option | Type | Necessity | Description | Default |
---|---|---|---|---|
| string | Required | Human-readable label that identifies the field type.
Value must be | |
| boolean | Optional | Flag that indicates whether Atlas Search recursively indexes all fields
and embedded documents. If set to To index all fields in a document including fields that Atlas Search doesn't dynamically index, define the fields in the index definition. If omitted or set to IMPORTANT: Atlas Search dynamically indexes all fields in a | false |
| document | Optional | Document that maps field names to field definitions. To learn
more, see an example. This is
required if |
Try an Example for the document
Type
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 index definition indexes the awards
field as the document
type. It also configures Atlas Search to automatically index all the
dynamically indexable fields inside the awards
object.
In the Add Field Mapping window, select awards from the Field Name dropdown.
Click the Data Type dropdown and select Document.
Toggle the Enable Dynamic Mapping setting to enable dynamic indexing of all dynamically indexable fields in the document, if it isn't already enabled.
Click Add.
Replace the default index definition with the following index definition.
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "awards": { 6 "type": "document", 7 "dynamic": true 8 } 9 } 10 } 11 }