Lesson 2: Dynamic vs. Static Mappings / Learn

Press O for more options
4:38

Video Transcript (English)

Atlas Search Indexes are the magic ingredient in Atlas Search.

When creating a search index, we need to provide some details on which fields need to be indexed.

This is known as a field mapping.

Having the correct field mappings can help with the performance of your search.

In this video, you’ll learn the difference between dynamic and static mappings. You’ll also learn how and when to use the appropriate mappings for your search indexes.

First, let's take a look at dynamic mappings.

When creating an index with dynamic mappings, we are indexing every field in the document that has a supported data type.

More on supported data types later.

Dynamic mappings are great for datasets that have unknown or frequently changing fields. They’re also useful when experimenting with Atlas Search so that you don’t have to constantly update your search index depending on the query.

This flexibility and power comes at a cost.

Dynamic mappings have increased size and performance impacts compared to static mappings.

Let’s apply dynamic mapping to our movie catalog app. Say we have a movie document with fields for the: Title, Plot, Genre, Directors, and release date.

To index every field in the movie collection, we use the mappings field in the index definition and set the dynamic option to true.

Since we didn’t provide a name for this index, it will be saved as “default”.

Now, each field in the movie collection document will be indexed. This means that we can create a search query for any of these fields.

Remember mongot? It keeps a watchful eye on any changes, so even if we were to add a new field for producers, it would be indexed automatically.

In addition to dynamic mappings, there are static mappings.

Static mappings index specific fields instead of the entire document.

We can reduce the size of our search indexes by using static mappings because they usually take up significantly less storage than a dynamically mapped index.

Moreover, because static mappings are focused on a known document structure they can avoid the computational overhead of scanning the entire document when changes are detected.

Therefore, static mappings can improve performance when the index needs to be updated.

Static mappings are ideal if we know the exact shape of our documents and do not anticipate the fields to change.

Let’s review the movie document from our previous example.

Instead of creating a dynamic mapping to index the entire document, we can create a static mapping on just the plot and released fields.

We anticipate searching our movie catalog frequently based on these fields.

With static mapping, we can only create search queries on the plot and released fields.

Let’s go ahead and build a new search index named plotReleasedIndex.

Next, we set the dynamic option to false.

Then, we define the fields that we want to index along with their data types.

In this case, the plot field is a string, and the released field is a date.

Keep in mind that MongoDB accommodates polymorphic data. If an attempt is made to insert a document with a field value data type differing from the one specified in the search index, that particular document won't be included in the index.

We’ll explore assigning multiple data types to a field in the future.

Now, let’s say we have a scenario where we need to use both dynamic and static mappings.

As an example, let's update the released field to hold an array of subdocuments.

Each subdocument holds a country code and the release date for that country.

We want to index the plot field and the entire released subdocument.

To do this, we set the dynamic option to false in the mappings.

Inside the fields option, we define the plot field and set its data type to string.

After that, we define the released field and assign it the embeddedDocuments data type because it's an array of subdocuments.

We make this field, and only this field, dynamic by setting the dynamic option to true.

Now both the plot field and all the release-date subdocuments are indexed. This makes it possible to perform search queries on them.

Great job! In this video, you learned that dynamic mappings index all fields in a document as long as the fields contain a supported data type.

You also learned that static mappings index specific fields instead of the entire document.

When deciding which mapping to use you must first consider whether or not the fields in your document will be changing frequently.

Use dynamic mappings if you don’t know the shape of your documents or anticipate them to change.

Keep in mind that dynamic mappings result in large search indexes and more computational resources to keep up to date.

Static mappings are ideal if we know the exact shape of our documents and do not anticipate the fields to change.

Since static mappings are focused on a known document structure they can avoid the computational overhead of scanning the entire document when changes are detected.