How to Index Date Fields
On this page
You can use the Atlas Search date
type to index date values. You can query
fields of type date
using the Atlas Search range, near,
and equals operators. To run a facet query on
date
fields, you must index the date fields using
dateFacet.
You can also use the date
type to index:
Fields whose value is an array of dates. To learn more, see How to Index the Elements of an Array.
Date fields inside an array of documents indexed as the embeddedDocuments type.
Date fields for sorting. Atlas Search automatically indexes all date fields in indexes created after July 2023 for sorting the Atlas Search results. For preexisting indexes, you must rebuild the index to use date fields in the index for sorting. To learn more, see Update Your Existing Index and Sort Atlas Search Results.
Note
If you enable dynamic mappings, Atlas Search
automatically indexes fields of type date
. You can
use the Visual Editor or the JSON Editor in the Atlas UI to
index fields as the date
type.
Define the Index for the date
Type
To define the index for the date
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 Mapping 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 Date. To learn more more about this type, see Field Properties.
Click Add.
The following is the JSON syntax for the date
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": "date" 7 } 8 } 9 } 10 }
Configure date
Field Properties
The Atlas Search date
type takes the following parameter:
Option | Type | Necessity | Description |
---|---|---|---|
| string | required | Human-readable label that identifies this field type. Value must
be |
Try an Example for the date
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 following example index definition indexes the released
field as the Atlas Search date
type to support queries against that
field using Atlas Search operators such as near,
range, and equals.
In the Add Field Mapping window, select released from the Field Name dropdown.
Click the Data Type dropdown and select Date.
Click Add.
Replace the default index definition with the following index definition.
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "released": { 6 "type": "date" 7 } 8 } 9 } 10 }
The following example index definition indexes the released
field as the date
and dateFacet
types to return the
following types of results for your queries:
Search results for queries using Atlas Search operators like near, equals, and range.
Metadata results for queries using Atlas Search facet.
In the Add Field Mapping window, select released from the Field Name dropdown.
Click the Data Type dropdown and select Date.
Click Add.
Repeat step 1 and select DateFacet from the Data Type dropdown.
Click Add.
Replace the default index definition with the following index definition.
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "released": [ 6 { 7 "type": "date" 8 }, 9 { 10 "type": "dateFacet" 11 } 12 ] 13 } 14 } 15 }