Video Transcript (English)
It’s time to zoom in on the various data types available at your disposal.
In this video, we’ll cover data types that are supported by Atlas Search. We’ll also define a search index with a mapping that uses multiple data types.
Before we begin, let’s recap how to set a field's data type in a search index.
After defining the mappings in a search index as static, we list the fields that we want to index, along with a type option for each.
The type option expects a data type supported by Atlas Search.
Atlas Search supports common data types such as Strings, Numbers, Booleans, and dates.
These data types map to their BSON counterparts.
For example, the number data type maps to the BSON types double, 32-bit and 64 bit integers.
Atlas Search also has other data types that work well with how documents are shaped, such as: Arrays, objectId, document, and embeddedDocuments.
Let’s explore each of these data types to get a better sense of what they do.
First, arrays can hold various data types such as strings, dates, objectIds, and numbers.
For example, let’s index the directors field, which is an array of strings.
We first define the directors field and assign it a type of string.
This is no different than indexing a field that is only a string.
There are a couple of types that aren’t allowed in arrays, such as dateFacet and numberFacet. We’ll learn more about these later.
Next, we have the objectId data type, which allows us to index document objectIds.
What happens if the field we want to search on is a subdocument?
For example, in the movies collection, there’s an “awards” field, which is a subdocument that holds information about how many times a movie has won or been nominated for an award. This is an ideal field for the document data type.
The document data type allows us to index fields that are a subdocument.
We can even index specific fields inside a subdocument.
However, the document data type can’t be used for fields that hold an array of subdocuments.
For example, if the release date field was an array of subdocuments, we would need to use the embeddedDocuments data type instead.
To index an array of subdocuments, use the embeddedDocuments data type.
Similar to the document data type, the embeddedDocuments data type has the option to index specific fields inside a subdocument.
Keep in mind that Atlas Search indexes embedded documents separate from their parent documents. This is important because it grows the number of documents you have in your index.
When using the embeddedDocuments field type, we can either dynamically map so that all of the supported data type fields are indexed, or we can statically map individual fields.
As an example, let’s index the date field inside the release date subdocument.
First, we need to define the “released” field and set its field type to embeddedDocuments.
Next, we set the dynamic option to false.
Then we define another fields option
Where we introduce the date field, along with its data type.
Now when we look at a movie document, we see that the date field inside the “released” subdocument is properly indexed.
Now that you know how the embeddedDocuments field type works, let's talk about its limitations.
First, it can’t be used on subdocuments with more than five levels of nesting.
It also can’t be used with fields that are date or numeric facets.
Finally, you can’t highlight fields that are embeddedDocuments.
Data types supported by Atlas Search can vary depending on whether you are using dynamic or static mappings.
For dynamic mappings, the following data types are supported: Strings, Numbers, Booleans, Arrays, objectId, Date, and documents.
And more will likely be added in the future.
For static mappings, most data types are supported. Check out the MongoDB documentation for a list of all supported data types.
In some cases, you may find yourself with a field that has multiple data types.
For example, let’s say we want to assign multiple data types when we index the directors field in the movies collection so that it can be either a name or an objectId.
To do this, we assign the directors field to an array of objects, with each object defined either as an objectId or string in a search index.
Now we can create a search query using a director’s name or objectId.
Awesome job! In this video, you learned about the data types supported by Atlas Search. These include: Strings, Numbers, Booleans, Arrays, objectId, Date, and documents.
You also learned that data type support in Atlas Search can vary depending on whether you are using dynamic or static mappings.
Finally, you learned that we can assign a field multiple data types when defining a search index.
