Docs Menu
Docs Home
/
MongoDB Atlas
/ / / /

How to Index Date Fields For Faceted Search

On this page

  • Review dateFacet Limitations
  • Define the Index for dateFacet Type
  • Configure dateFacet Field Properties
  • Try an Example for dateFacet Type

You can use the Atlas Search dateFacet type for indexing date values for faceting.

Atlas Search only supports facet queries against fields indexed as the dateFacet type. To perform a normal search also on the same field, you must index the field as type date also.

To facet on string fields in embedded documents, you must index the parent fields as the document type. When you facet on a string field inside embedded documents, Atlas Search returns facet count for only the number of matching parent documents.

Atlas Search doesn't dynamically index date values for faceting. You must use static mappings to index date values for faceting. You can use the Visual Editor or the JSON Editor in the Atlas UI to index date fields as the numberFacet type.

The following limitations apply:

  • You can't index a date field for faceting if it's inside an array or if it's inside a document in an array.

  • Atlas Search doesn't support date faceting over fields indexed as part of an embeddedDocuments field.

    Note

    To vote for this feature and provide your feedback, use this MongoDB feedback article.

To define the index for the dateFacet 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 DateFacet. To learn more more about this type, see Field Properties.

  6. Click Add.

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

{
"mappings": {
"dynamic": true|false,
"fields": {
"<field-name>": {
"type": "dateFacet"
}
}
}
}

The Atlas Search dateFacet type takes the following parameter:

UI Field Name
JSON Option
Type
Necessity
Description

Data Type

type

string

required

Human-readable label that identifies this field type. Value must be dateFacet.

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 dateFacet type to support queries against that field using Atlas Search facet.

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

  2. Click the Data Type dropdown and select DateFacet.

  3. Click Add.

Replace the default index definition with the following index definition.

{
"mappings": {
"dynamic": false,
"fields": {
"released": {
"type": "dateFacet"
}
}
}
}

The following example index definition indexes the released field as the dateFacet and date types to return the following types of results for your queries:

  • Metadata results for queries using Atlas Search facet.

  • Search results for queries using Atlas Search operators like near, equals, and range.

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

  2. Click the Data Type dropdown and select DateFacet.

  3. Click Add.

  4. Repeat step 1 and select Date from the Data Type dropdown.

  5. Click Add.

Replace the default index definition with the following index definition.

{
"mappings": {
"dynamic": false,
"fields": {
"released": [
{
"type": "dateFacet"
},
{
"type": "date"
}
]
}
}
}

Tip

See also: Additional Index Definition Examples

Back

date