number-facet-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 Numeric Values for Faceted Search
You can use the MongoDB Search numberFacet type to index numeric values using the specified representation for faceting. You can index numbers of BSON (Binary Javascript Object Notation) types int32, int64, and double.
MongoDB Search only supports [facet](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-ref operator queries against fields indexed as the numberFacet type. To perform a normal search also on the same field, you must index the field as type number also.
To facet on string fields in embedded documents, you must index the parent fields as the [document](https://www.mongodb.com/docs/search/indexes/field-types/document-type.md#std-label-bson-data-types-document type. When you facet on a string field inside embedded documents, MongoDB Search returns facet count for only the number of matching parent documents.
MongoDB Search doesn't [dynamically](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings index number values for faceting. You must use [static mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings to index number values for faceting. You can use the Visual Editor or the JSON Editor in the Atlas UI to index number fields as the number type.
numberFacet Type Limitations
The following limitations apply:
-
You can't index
decimal128for faceting. -
You can't index numeric values in arrays or in a document contained in an array for faceting.
-
You can't facet over numeric fields indexed as part of an
embeddedDocumentsfield.
Define the Index for the numberFacet Type
To define the index for the numberFacet type, choose your preferred configuration method in the Atlas UI and then select the database and collection.
Visual Editor
-
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 NumberFacet.
-
Configure the field properties for the
numberFacettype. To learn more, see [Field Properties](https://www.mongodb.com/docs/search/indexes/field-types/uuid-type.md#std-label-fts-field-types-uuid-options. -
Click Add.
Configure numberFacet Field Properties
The MongoDB Search numberFacet type takes the following parameters:
| Option | Type | Necessity | Description | Default |
|---|---|---|---|---|
type |
string | Required | The type of field. Value must be numberFacet. |
|
representation |
string | Optional | The data type of the field to index. Values can be one of the following BSON (Binary Javascript Object Notation) types: int64 - for indexing large integers without loss of precision and for rounding double values to integers. You can't use this type to index large double values.; double - for indexing large double values without rounding. To learn more, see [example](https://www.mongodb.com/docs/search/indexes/field-types/number-facet-type.md#std-label-number-facet-egs below. |
double |
indexIntegers |
boolean | Optional | Indicates whether to index or omit indexing int32 and int64 type values. Value can be true or false. Either this or indexDoubles must be true. |
true |
indexDoubles |
boolean | Optional | Indicates whether to index or omit indexing double type values. Value can be true or false. Either this or indexIntegers must be true. |
true |
Try an Example for the numberFacet Type
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.
Basic Example
The following example index definition indexes the year field as the MongoDB Search numberFacet type to supports queries against that field using MongoDB Search [facet.](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-ref
Visual Editor
-
In the Add Field Mapping window, select year from the Field Name dropdown.
-
Click the Data Type dropdown and select NumberFacet.
-
Accept the default values for the NumberFacet Properties.
-
Click Add.
Multiple Types Example
The following example index definition indexes the year field as the numberFacet and number types to return the following types of results for your queries:
-
Metadata results for queries using MongoDB Search [
facet(MongoDB Search Operator).](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-ref -
Search results for queries using MongoDB Search operators like [near](https://www.mongodb.com/docs/search/query/operators-collectors/near.md#std-label-near-ref, [equals](https://www.mongodb.com/docs/search/query/operators-collectors/equals.md#std-label-equals-ref, and [range.](https://www.mongodb.com/docs/search/query/operators-collectors/range.md#std-label-range-ref
Visual Editor
-
In the Add Field Mapping window, select year from the Field Name dropdown.
-
Click the Data Type dropdown and select NumberFacet.
-
Accept the default values for the NumberFacet Properties.
-
Click Add.
-
Repeat step 1 and select Number from the Data Type dropdown.
-
Accept the default values for the Number Properties.
-
Click Add.
Learn More
To learn more about the facet collector and see example queries, see [Examples.](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-egs
To learn how to create an index with a facet definition and query that index using the facet collector, see the [How to Use Facets with MongoDB Search](https://www.mongodb.com/docs/search/tutorial/facet-tutorial.md#std-label-facet-tutorial tutorial.
