Docs Menu
Docs Home
/
MongoDB Atlas
/ / / /

How to Index ObjectId Values in Fields

On this page

  • Define the Index for the objectId Type
  • Configure objectId Field Properties
  • Try an Example for the objectId Type

You can use the Atlas Search objectId type to index ObjectId values. You can query fields of type objectId using the equals and in operators.

You can also use the objectId type to index:

If you enable dynamic mappings, Atlas Search automatically indexes fields of type objectId. You can use the Visual Editor or the JSON Editor in the Atlas UI to index fields as the objectId type.

To define the index for the objectId 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. 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.

  4. Click the Data Type dropdown and select ObjectId. To learn more more about this type, see Field Properties.

  5. Click Add.

The following is the JSON syntax for the objectId 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": "objectId"
}
}
}
}

The Atlas Search objectId type takes the following option:

UI Field Name
JSON Option
Type
Necessity
Description
Data Type
type
string
required
Human-readable label that identifies this field type. Value must be objectId.

The following index definition example uses the sample_mflix.comments 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 movie_id field as the objectId data type to support queries against that field using the Atlas Search equals operator.

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

  2. Click the Data Type dropdown and select ObjectId.

  3. Click Add.

Replace the default index definition with the following index definition.

{
"mappings": {
"dynamic": false,
"fields": {
"movie_id": {
"type": "objectId"
}
}
}
}

Tip

See also: Additional Index Definition Examples

Back

numberFacet

Next

string