Event
{Event}  MongoDB is going on a world tour! Gather your team and head to your nearest MongoDB.local. Learn more >

Search Indexes

Whether you are building an e-commerce website, a game, or a ride-hailing application, all modern applications require some sort of data. And the more data there is, the longer it might take to parse all of it to find a specific piece of information. This is where search indexes come into play. Having good search indexes is crucial in optimizing a database for fast and efficient queries.

What is a search index?

A search index is a way to categorize data in an easily searchable format. One example is how librarians use the Dewey Decimal classification to store non-fiction books. Books on a specific topic are grouped and assigned numbers that follow a particular sequence. If you look for a book on celestial mechanics, it starts with 521.x. To consult it, you will need to look after the books about languages (400) and before the books on technology (600). However, fiction books use a different index. They are usually organized by author in alphabetical order. This grouping makes it easier for users to find related books if they like an author. Classifying by title or release date would make no sense for a user browsing the bookshelves.

In the same way, search indexes are a way to find data in an electronic context. Data is stored in different ways in databases, based on the type of database you use. Without an index, the database needs to browse all the records to find a specific record. The fields you regularly use to filter the data should be indexed to help the database find the information quicker.

Keep in mind that indexes also come at a cost. Building indexes can be time-consuming, and take up a lot of your resources. Additionally, the database will need to store those indexes somewhere, and this takes up more disk space.

Search index examples

You can use search indexes in many different ways in a database. Most importantly, fields used frequently for sorting or filtering should be indexed.

If you have a collection of documents for all of your invoices, chances are that you will want to query those invoices using the issued date. You will most likely want to query all of your invoices to get your quarterly sales totals every quarter. To help your database find those invoices faster, you could add a search index to the issued date field. The database will create an index where all documents are ordered by the date field. This way, the database engine will only need to go through the documents related to the last quarter and stop querying once it reaches the beginning of the quarter.

You might regularly want to see which of your customers made the most significant orders in this same collection. In addition to the date index, you could add an index on the total invoice amount. This way, the database engine will easily find the top largest invoices and return those to you quickly, without looking at all individual documents.

Search index and MongoDB

In MongoDB, just like any other database management system, indexes are used to increase your database performance. You can create indexes on any field. Indexes can be on a single field, multiple fields, or even wildcards for arrays.

The Performance Advisor available in MongoDB Atlas can provide you with recommendations on which collections and fields would benefit from indexing. Using those recommendations can significantly improve the performance of your database and reduce the costs associated with inefficient hardware resources usage.

In addition to regular indexes, MongoDB Atlas offers full-text search capabilities. Like traditional search indexes, full-text search indexes help speed up the queries on text fields. You can enable Atlas Search with a few clicks from the Atlas UI or a single call to the Administration API.

Using Atlas Search will let you perform queries using the same MongoDB Query API you are already familiar with to return relevant results. You can even add fuzzy text searching or lists of synonyms to deliver accurate results to your users every time.

To learn more about Atlas Search, you can watch the introduction video or check out the MongoDB Atlas Search page for more information.

In summary

Search indexes are essential to ensure a good performance from your database. They guarantee that the data is easy to find and that your hardware resources aren’t used inefficiently. In addition to traditional search indexes, MongoDB Atlas offers you text search indexes. These indexes will help you add full-text search capabilities to your application to create a better user experience for your customers. You can try out Atlas Search in MongoDB Atlas for free today.

Many more hands-on examples and tutorials are available on the MongoDB Dev Hub.

FAQ

What is an index in searching?

An index is similar to a list of all the documents in a collection ordered by one or more specific fields. This helps the database engine go down the list until it finds the right match rather than searching each document.

What does indexing mean?

Indexing is the action of adding search indexes to a database. Adding search indexes will help your database perform better and save you money on hardware resources that would otherwise be used to do useless searches.

What does a search index do?

A search index will let a database engine organize data more efficiently for querying. It creates an ordered list of values and the associated documents to quickly go down the list instead of looking at each document in the database.

What is an example of an index?

If you have a collection of documents with information about customers, creating an index on the Country field from the customer address would lead to faster results when querying your customer list by country.

What is Atlas Search index?

Atlas Search uses a unique index to organize all the text data from your collection so that it can be easily queried. An analyzer will be used to index the specified fields, letting you use the MongoDB Query API to find relevant data based on the requested content.
MongoDB Atlas Search is a feature in Atlas that lets you create search indexes on large text fields. Atlas Search is highly configurable and lets you fine tune the results so that your users always get the most relevant results.

How do I search in MongoDB?

When using MongoDB Atlas, you can use Atlas Search to create search indexes. Data can then be searched using the MongoDB Query API.