Text Search: Part 1
Explore how to use MongoDB's text search feature by creating text indexes on one or more fields. Learn to perform searches with the $text operator, including phrase matching, case sensitivity options, and exclusion of words. Understand how to optimize queries using text indexes and handle common scenarios for full-text search in MongoDB.
We use the text search operation to search words, phrases, or words with conditions against text data stored in one or more fields. MongoDB provides text search with the help of text index and supports multiple languages. This is also called a full-text search.
First, a text index is created on one or more fields, and then with help of the $text operator text search is performed.
We can only use one text index on a collection.
Note: The fields should have string content.
Create text index
To perform the text search, the field should have a text index on the field.
Text index on one field
Below is the syntax to create a text index on a field of the document.
db.<collection-name>.createIndex({
<field-name>: "text",
...
});
Let’s create a text ...