Visit complete MongoDB roadmap

← Back to Topics List

Text

MongoDB provides a powerful feature called “Text Indexes” to enable searching for string content within documents. This comes in handy when you need to perform text searching and analysis in your MongoDB collections. A Text Index allows you to search for words, phrases or even complex query expressions with ease.

Creating a Text Index

To create a Text Index, use the db.collection.createIndex() method along with the special index type: { fieldName: "text" }. For example, to create a Text Index on the title field in a books collection, execute the following command:

db.books.createIndex({ title: 'text' });

Perform Text Searches

After creating the Text Index, you can perform text searches on your documents using the $text operator in your queries inside db.collection.find(). For example, to find all books with a title matching the words “mongodb” or “guide”, execute the following command:

db.books.find({ $text: { $search: 'mongodb guide' } });

Advanced search options

MongoDB provides several advanced options to refine your text search:

  • $language: Specify the language for your search query. Useful for stemming and ignoring stop words.

  • $caseSensitive: Enable or disable case-sensitive search (false by default).

  • $diacriticSensitive: Enable or disable diacritic-sensitive search (false by default).

Dropping a Text Index

If you no longer need the Text Index, you can drop it using the db.collection.dropIndex() method. You’ll need to provide the index name as the parameter:

db.books.dropIndex('title_text');

Text Indexes provide an efficient way to search for content within your MongoDB documents, making it easier to analyze and locate specific information. However, it’s important to keep in mind that Text Indexes can slow down your write performance, so use them judiciously!

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.