How to Create a Search Index with Dynamic Field Mapping in MongoDB
Last Updated :
09 Apr, 2024
Dynamic mapping in MongoDB Atlas Search is a powerful feature that automatically indexes all supported field types in a collection and making it ideal for applications with evolving data models. This type of mapping is particularly useful when the schema is unknown or changes regularly. Dynamic mappings assign fields automatically when new data is inserted, simplifying the setup for diverse data structures.
In this article, We will learn about, What is Dynamic Mapping, Need of Dynamic Field Mapping for Search Indexes, When to Use Dynamic Mapping and how to create Search Index for Dynamic Mapping Using Mongosh or MongoDB Driver detail.
What is Dynamic Mapping?
- Dynamic mappings in MongoDB Atlas Search can automatically index all supported field types in a collection. This can be done by defining the collections (field mapping) as dynamic.
- Dynamic mappings can be used when the schema is unknown or changes regularly, or when experimenting with Atlas Search.
- Dynamic mappings assign fields automatically when new data is inserted. we can use dynamic mappings to configure an entire index or specify individual fields, such as fields of type document, to be dynamically mapped.
- It is the fastest and easiest way to get started with searching data. We can use dynamic mapping if the data schema constantly changes, when the exact structure is unknown, or when the collection contains a mix of document types with various field names.
Note: All the fields in a MongoDB collection are Indexed (except boolean, objectID and timestamps).
Why Use Dynamic Field Mapping for Search Indexes?
- Dynamic field mapping allows the index to adapt to changes in the document structure without manual intervention.
- It solves the indexing process by automatically incorporating new fields as they appear in documents.
- Dynamic field mapping is great for apps that have changing data structures..
When to Use Dynamic Mapping?
- When data schema is constantly changing as dynamic mapping adapts automatically to new fields, saving us to do manual updates.
- When there is a mix of document types because it has a simplified setup for diverse data, automatically handles different field structures.
- When we need a quick search setup where we can get started quickly without defining every field mapping.
Dynamic Field Mapping Strategies
1. Default Dynamic Field Mapping: MongoDB's default dynamic field mapping automatically indexes all fields in the document.
2. Custom Dynamic Field Mapping: Define specific fields for dynamic mapping using the $text index type. We can create a custom dynamic index on `authors` collection and view the search index created using the command "db.authors.getIndexes()".

Prerequisites
- We should know how to connect MongoDB using a connection String when working with MongoDB driver (MongoDB version 4.2 or higher).
- A Collection to create a Atlas Search Index.
Steps for creating search index with dynamic field mapping In MongoDB ATLAS:
- Connect to MongoDB By logging into our MongoDB Atlas Database.
- Select project your desired project from the Projects menu in the navigation bar.
- Click your cluster's name.
- Click the Atlas Search tab.
- Click Create Search Index. As shown in Fig: 1.1.
Fig: 1.1
6. Select an Atlas Search Configuration Method and click Next.
a) For a guided experience, select the Atlas Search Visual Editor.
b) To edit the raw index definition, select the Atlas Search JSON Editor.
7. Enter the Index Name, In the Index Name field, enter default. (Note: If you name your index default, we don't need to specify an index parameter when using the $search pipeline stage. Otherwise, we must specify the index name using the index parameter.) and then Click Next.
a) Visual Editor
Click Next. Review the `default` index definition for the books collection.
b) JSON Editor
Click Next. Review the index definition. Your index definition should look similar to the following (Fig 1.2) and Click Next.
8. Set the Database and Collection. As per here, we are considering “Aggregation_MDB" database and "books" collection within it.
9. Specify an index definition by use the Atlas Search Visual Editor or the Atlas Search JSON Editor in the Atlas user interface to create the index. For Example: We define index definition as dynamic for the books collection.
Fig: 1.2
10. Click Next
11. Review the index definition. Our index definition should look similar to the following (Fig: 1.3).
Fig: 1.3
12. Click Save Changes.
13. Click Create Search Index.
14. Close the You're All Set! Modal Window.
15. Click the Close button. Wait for the index to finish building. The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.
Search Index with dynamic mapping for books collection
Creating Search Index for Dynamic Mapping Using Mongosh or MongoDB Driver:
- Create a file named `create-index.js`.
- Copy and paste the following code into the `create-index.js` file.
const { MongoClient } = require("mongodb");
// connect to your Atlas deployment
const uri =
"<connection-string>";
const client = new MongoClient(uri);
async function run() {
try {
// set namespace
const database = client.db("Aggregation_MDB");
const collection = database.collection("books");
// define your Atlas Search index
const index = {
name: "default",
definition: {
/* search index definition fields */
"mappings": {
"dynamic": true
}
}
}
// run the helper method
const result = await collection.createSearchIndex(index);
console.log(result);
} finally {
await client.close();
}
}
run().catch(console.dir);
This code creates a search index named "default" in the "books" collection of a MongoDB Atlas database. The key point is `mappings: { dynamic: true }`, which enables dynamic field mapping for the search index. This lets Atlas Search automatically detect and index supported fields in your documents. The `collection.createSearchIndex(index)` method is called. This method takes the defined search index object (`index`) as an argument and creates the search index within the specified collection.
3. Replace the `<connection-string>` in the query and then save the file. Ensure that your connection string includes your database user's credentials.
4. Create the index by run the following command and the output will be name of index field, which is `default` for this particular example.
Output
Advantages of Using Dynamic Mapping
- Dynamic field mapping allows indexes to adapt to the evolving structure of the document without direct involvement, making it suitable for application with dynamic schema.
- It simple the indexing process by automatically detecting and indexing the new fields that appear in the document, making it easy to get started with running search queries.
- Since dynamic mapping adapts automatically to the changing schema, it reduces the need for constantly updating the search index definition.
Disadvantages of Using Dynamic Mapping
- Limited Control: Dynamic mapping in MongoDB offers less control over the indexing process compared to static mapping.
- Irrelevant indexes: Large number fields are indexed in the document regardless of whether they are relevant or not.
- Performance Considerations: It added extra processing time by determining the data types during indexing and querying.
- Extra Space: It takes more disk space and might negatively impacts the cluster performance.
- Potential for Inaccuracy: Since dynamic mapping automatically detects field types, there's a chance it might misinterpret certain data types, leading to less accurate search results.
Best Practices for Using Dynamic Field Mapping
- Monitor Performance: Use MongoDB profile to monitor and optimize the index performance.
- Analyze Schema Changes: Even with dynamic mapping, analyze any major schema changes to ensure they're being indexed appropriately.
- Thoughtfully Choose Index Fields: To avoid necessary overheads, select relevant fields for indexing.
- Levages compound indexes: Consider creating compound indexes to leverage the search performance.
- Optimize query performance: Optimize query performance by denormalizing data across multiple documents and creating indexes to support queries.
Comparsion between Dyanmic and Static Mapping
| Dynamic Mapping
| Static mapping
|
---|
1
| It is suitable for evolving schema.
| It Is less adaptable to dynamic schema.
|
2
| Less control over the indexing process by automatically indexing all the fields in the documents.
| It has Granular control over the indexing process by allowing to explicitly define which fields to be indexed.
|
3
| It automatically adapts to the changes in the documents structure.
| It requires manually indexing the relevant fields of the document.
|
4
| It requires more disk space, as it indexes all the fields regardless of whether there are relevant or not.
| It can be optimized for better performance by improving the size and structure of the search index for the relevant fields in the doscuments.
|
Conclusion
Overall, Dynamic mapping in MongoDB Atlas Search provides a flexible and efficient approach to indexing data, particularly for applications with evolving schemas. It eliminates the need for manual updates and fast the indexing process by automatically adapting to changes in the document structure. Along with its advantages, dynamic mapping offers less control over the indexing process compared to static mapping.
Similar Reads
How to Find Items Without a Certain Field in MongoDB
In MongoDB, querying for documents that don't have a certain field can be a common requirement, especially when dealing with schemaless data. While MongoDB provides various querying capabilities, finding documents without a specific field can sometimes be difficult. In this article, we'll explore di
4 min read
How to Create Indexes in MongoDB using Node.js?
MongoDB, a popular NoSQL database, provides powerful indexing capabilities to improve query performance. Indexes in MongoDB help in quickly locating documents and speeding up read operations. In this tutorial, we'll explore how to create indexes in MongoDB using Node.js. What is an Index in MongoDB?
3 min read
Creating Multi-Field Indexes in MongoDB
In MongoDB, indexes play an important role in improving query performance by efficient data retrieval. While single-field indexes are useful for optimizing queries on individual fields, multi-field indexes are designed to enhance queries that involve multiple fields. In this article, we will learn t
4 min read
How to Create and Validate JSON Schema in MongoDB?
JSON Schema validation in MongoDB allows you to enforce the structure of documents in a collection. This ensures data integrity by validating documents against defined schemas before they are inserted or updated. In this article, we will cover how to create and validate JSON Schema in MongoDB using
5 min read
How to Search by id in MongoDB
In MongoDB, each document in a collection is uniquely identified by a field called "_id". This "_id" field serves as the primary key and provides a unique identifier for each document. Searching by ID is a common operation in MongoDB and allows us to retrieve specific documents based on their unique
4 min read
How to Filter Array in Subdocument with MongoDB?
In MongoDB, working with arrays within subdocuments is a common requirement in many applications. Filtering and manipulating arrays efficiently can significantly enhance the flexibility and enhance our queries. In this article, we'll explore how to filter arrays within subdocuments in MongoDB by cov
5 min read
How to Find Document with Array that Contains a Specific Value in MongoDB
MongoDB is a popular choice for developers working with large volumes of data, due to its flexibility and powerful querying capabilities. One common task developers face is finding documents in a collection where an array field contains a specific value. In this beginner-friendly article, we'll expl
4 min read
How to Update the First Object in an Array in MongoDB
MongoDB, a popular NoSQL database, offers powerful features for handling complex data structures. One common scenario is updating specific elements within arrays stored in documents. In this guide, we'll focus on updating the first object in an array within a MongoDB document. We'll cover the concep
3 min read
How to do a Full-Text Search in MongoDB using Mongoose
In MongoDB, performing a full-text search allows us to query and retrieve documents based on textual content matching certain criteria. When using Mongoose, an ODM (Object Data Modeling) library for MongoDB in Node.js, conducting full-text search operations can be efficiently achieved using the Mong
5 min read
How to Manage Data with MongoDB
Effective data management is critical for the success of any application. MongoDB, a leading NoSQL database, offers a flexible and scalable solution for handling unstructured and semi-structured data. In this article, We will go through How To Manage Data with MongoDB by understanding its data model
4 min read