You can use the MongoDB Search numberFacet type to index numeric values using
the specified representation for faceting. You can index numbers of
BSON types int32, int64, and double.
MongoDB Search only supports facet 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 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 index
number values for faceting. You must use static
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.
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.Click Add.
The following is the JSON syntax for the numberFacet 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": "numberFacet", "representation": "int64|double", "indexIntegers": true|false, "indexDoubles": true|false } } } }
Configure numberFacet Field Properties
The MongoDB Search numberFacet type takes the following parameters:
Option | Type | Necessity | Description | Default |
|---|---|---|---|---|
| string | Required | The type of field. Value must be | |
| string | Optional | The data type of the field to index. Values can be one of the following BSON types:
To learn more, see example below. |
|
| boolean | Optional | Indicates whether to index or omit indexing |
|
| boolean | Optional | Indicates whether to index or omit indexing |
|
Try an Example for the numberFacet 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 year field
as the MongoDB Search numberFacet type to supports queries against
that field using MongoDB Search facet.
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.
Replace the default index definition with the following index definition.
{ "mappings": { "dynamic": false, "fields": { "year": { "type": "numberFacet" } } } }
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).Search results for queries using MongoDB Search operators like near, equals, and range.
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.
Replace the default index definition with the following index definition.
{ "mappings": { "dynamic": false, "fields": { "year": [ { "type": "numberFacet" }, { "type": "number" } ] } } }
Learn More
To learn more about the facet collector and see example queries, see Examples.
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 tutorial.